Writing a class in a Microsoft Access database...

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 am 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
 
N

Norman Yuan

VB/VBA does not support overload, so, you cannot have both

Property Get Buildings(index As Integer) As Building

and

Property Get Buildings() As Buildings()

One solution is to use collection of Building, insdead of array of Building.
Collection provides Building.Item(index/key) (or Building(index/key)) method
and .Count property for you. Of course, if you have a lot of Buildings in a
Town, processing array could be faster than processing collection.

The other solution is to provide a read-only Count property to the Building
class, or to give a different name to the Building array property:

Property Get Count() As Integer
Count=Ubound(pBuildings)+1
End Property

or

Property Get BuilingArray() As Buildings
BuildingArray=pBuildings
End Property
 

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