ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Determine the class of an object (https://www.excelbanter.com/excel-programming/304503-determine-class-object.html)

Edward Ulle

Determine the class of an object
 
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







*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

keepITcool

Determine the class of an object
 
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



--
keepITcool

| www.XLsupport.com | keepITcool chello nl | amsterdam

Edward Ulle

Determine the class of an object
 

Yes thank you.

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

Thanks again.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jamie Collins

Determine the class of an object
 
Edward Ulle wrote ...

I construct several classes and store them sequentually in a collection
as they are created.


Multiple class types in the same collection? This is a stab in the
dark but you may be interested in VBA's version polymorphism. See:

http://msdn.microsoft.com/library/de...ismallthat.asp

Jamie.

--

Jamie Collins

Determine the class of an object
 
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.

--

keepITcool

Determine the class of an object
 
Jamie Collins wrote :

... I use a fabricated ADO recordset myself, of course <g).

Jamie.

--


shaped where possible :)

--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam

Jamie Collins

Determine the class of an object
 
"keepITcool" wrote ...

... I use a fabricated ADO recordset myself, of course <g).


shaped where possible :)


Actually, my object models are hierarchical, not my collection classes <g.

Jamie.

--


All times are GMT +1. The time now is 05:58 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com