Property Let Get Set...

J

J S

I'm writing a "Town" class and I'm trying to figure out the correct way to
write it. I have a private array of "Building" objects in the "Town" class.
I have written a Get and Set procedure to access the array and they work
fine.

The following is a partially defination of the Class Town.
----------------------------------------------------------
Private pBuildings()
----------------------------------------------------------
Public Property Get Buildings(ByVal Index As Integer) As Building
Set Buildings = pBuildings(Index)
End Property
----------------------------------------------------------
Public Property Set Buildings(ByVal Index As Integer, ByVal Value As
Building)
Set pBuildings(Index) = Value
End Property
----------------------------------------------------------

Now I'm running into a problem using the following syntax

UBound(iTown.Buildings) --Which should return the Ubound of the Private
pBuildings array.

Where iTown is a Town Object and I trying to figure out how many buildings I
have in the town.

The error message says something like "argument not optional". Which I
assume is caused by the fact the property GET procedure is looking for a
index variable. I want to find a way to write the class so that my code does
not need to change and Ubound(iTown.Buildings) syntax continues to work.

I was thinking along the lines of trying to figure out how to overload the
property GET procedure with something like this
 
J

Jonathan West

Hi J S

J S said:
I'm writing a "Town" class and I'm trying to figure out the correct way to
write it. I have a private array of "Building" objects in the "Town" class.
I have written a Get and Set procedure to access the array and they work
fine.

The following is a partially defination of the Class Town.
----------------------------------------------------------
Private pBuildings()
----------------------------------------------------------
Public Property Get Buildings(ByVal Index As Integer) As Building
Set Buildings = pBuildings(Index)
End Property
----------------------------------------------------------
Public Property Set Buildings(ByVal Index As Integer, ByVal Value As
Building)
Set pBuildings(Index) = Value
End Property
----------------------------------------------------------

Now I'm running into a problem using the following syntax

UBound(iTown.Buildings) --Which should return the Ubound of the Private
pBuildings array.

Not the way you have written it. The Buildings property is returning a
single Building object, as determined by the mandatory Index parameter that
you must supply when calling it.

If you want to get the number of items in the array, then I would recommend
you and a new property procedure, as follows

Public Property Get CountBuildings() As Integer
CountBuildings = Ubound(pBuildings)
End Property


Where iTown is a Town Object and I trying to figure out how many buildings I
have in the town.

The error message says something like "argument not optional". Which I
assume is caused by the fact the property GET procedure is looking for a
index variable. I want to find a way to write the class so that my code does
not need to change and Ubound(iTown.Buildings) syntax continues to work.

Using the above, you have to change that code do that instead of
Ubound(iTown.Buildings), you instead have iTown.CountBuildings. That should
be a simple search & replace.


I was thinking along the lines of trying to figure out how to overload the
property GET procedure with something like this

You can't overload properties in VBA.
 

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