Problem accessing class subroutine

  • Thread starter Steven M (remove wax and invalid to reply)
  • Start date
S

Steven M (remove wax and invalid to reply)

I have a couple of classes, including PointClass:

Private X As Double
Private Y As Double

Another is myListClass. Its purpose is to manage a list containing a
variable number of points:

Private mCurrentSize As Long
Private mMaxSize as Long
Private mList() As PointClass

' mCurrentSize is the number of points currently stored
' mMaxSize is the current size of array mList

==========
Next, I create this subroutine in the class, to add a new point to the
list:
==========

Public Sub AddPoint (NewPoint As PointClass)

If mCurrentSize = mMaxSize Then
ReDim Preserve mList (2 * mCurrentSize)
mMaxSize = mMaxSize * 2
End If
mCurrentSize = mCurrentSize + 1

mList(mCurrentSize).X = NewPoint.X
mList(mCurrentSize).Y = NewPoint.Y

End Sub

==========
Then I created this test stub. It compiles and starts to run, but ...
==========

Sub stub()

Dim thisList As myListClass
Dim TestPoint As PointClass

Set thisList = New myListClass
Set TestPoint = New PointClass

TestPoint.X = 300
TestPoint.Y = 500

' fails on this line:
thisList.AddPoint (TestPoint)

End Sub

==========
It fails when it calls "AddPoint". The error message is:

- - - - - - - - - - - -
Run-time error '438':
Object doesn't support this property or method
- - - - - - - - - - - -

Why doesn't this work? How can I fix it?

Do I need to use a "New" statement to initialize the mList array?

Do I need a constructor for myListClass? I tried this, but it didn't
eliminate the error:

==========
Private Sub Class_Initialize()
mCurrentSize = 0
End Sub
==========

Is there a better approach?

Thanks.



--
Steven M - (e-mail address removed)
(remove wax and invalid to reply)

bus station = where bus stops
train station = where train stops
work station = ?
 
J

Jonathan West

Hi Steven

Its not all that clear from your post what code is in which class, and so it
is not easy to be sure of the source of your problem. but if these lines

Private X As Double
Private Y As Double

are in PointClass, then the problem is a Private keyword, which I suspect
should be Public, so that the variables are exposed as properties of the
class.

If those 2 lines are the only thing in the class then you would probably be
better off not declaring a class module, but instead declaring a
user-defined type, like this

Public Type PointClass
X as Double
Y as Double
End Type


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
S

Steven M (remove wax and invalid to reply)

Je Wed, 26 Jul 2006 09:32:54 EDT, Steve Rindsberg
Use either

Call thisList.AddPoint (TestPoint)

or

thisList.AddPoint TestPoint

Thanks, that was it.


--
Steven M - (e-mail address removed)
(remove wax and invalid to reply)

"Patriotism means to stand by the country. It does not mean
to stand by the president or any other public official save
exactly to the degree in which he himself stands by the country."
-- Theodore Roosevelt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top