Determine the class of an object

E

Edward Ulle

I construct several classes and store them sequentually in a collection
as they are created. Each class is unique. To access a method of the
class I need to cast back to the original class type.

How can I determine the class type during runtime? See the following
pseudo code.

Dim myCollection as New Collection
Dim myClass1 as Class1
Dim myClass2 as Class2

Set myClass1 = new Class1
myCollection.Add Item:=myClass1
Set myClass2 = new Class2
myCollection.Add Item:=myClass2
set myClass1 = new Class1
myCollection.Add Item:=myClass1

Later

Dim nowClass1 as Class1
Dim nowClass2 as Class2

Set nowClass1 = myCollection.Item(1) ' Same class
Set nowClass2 = myCollection.Item(1) ' Not the same class
 
K

keepITcool

Edward Ulle wrote :
I construct several classes and store them sequentually in a
collection as they are created. Each class is unique. To access a
method of the class I need to cast back to the original class type.

How can I determine the class type during runtime? See the following
pseudo code.

Edward do you mean this?

Select case TypeName(myCollection(1))
case "Class1"
Set nowClass1 = myCollection(1)
case "Class2"
Set nowClass2 = myCollection(1)
End Select
 
E

Edward Ulle

Yes thank you.

I did not look at TypeName because I thought it only applied to user
defined types not classes.

Thanks again.
 
J

Jamie Collins

Edward Ulle wrote ...
Set myClass1 = new Class1
myCollection.Add Item:=myClass1
Set myClass2 = new Class2
myCollection.Add Item:=myClass2

Another thought: a Collection object has no functionality to retrieve
the key that was used to add an item. You may prefer to use an
'container' object that does e.g. a Dictionary object (I use a
fabricated ADO recordset myself, of course <g>).

Jamie.

--
 

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