Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

this or self for an class instance in VB/VBA

Status
Not open for further replies.

bojisheng

Programmer
May 16, 2002
15
0
0
US
I have created a class that I want to manage its instances via a collection (I am using VBA so no Dictionary). I would like an instance to check a collection and add itself to the collection (if it does not exist). I have not found a way to reference the instance while within a method.

This would be like 'this' in C or 'self' in Smalltalk. Does anyone know how I would do this in VB/VBA? I have not found anything in the manuals as yet.
 
I usually just create another class that contains the collection and have that collection class create the new instance of the class and add it to the collection. Something like this
Code:
Private mCol As Collection

Public Function Add(Property1 As String, _
                    Property2 As String, _
                    Property2 As Single, _
                    ... etc. ..., _
                    Optional sKey As String) As myClass
                    
    'create a new object
    Dim NewClass As myClass
    Set NewClass = New myClass

    'set the properties passed into the method
    NewClass.Property1 = Property1
    NewClass.Property2 = Property2
    NewClass.Property3 = Property3
    ... etc. ...

    If Len(sKey) = 0 Then
        mCol.Add NewClass
    Else
        mCol.Add NewClass, sKey
    End If

    'return the object created
    Set Add = NewClass
    Set NewClass = Nothing

End Function
You will need to add the other methods for managing the collection (Item, Count, Remove, Enum, etc.) to this collection class.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top