How To Determine An Object's Class ?

R

Russ McKenna

What kind of object, what kind of class?
More info please. What are you trying to do?
 
D

Dennis

I have a collection of objects that were instantiated from
classes that I've wriiten. I need to go through the
collection looking for objects from specific classes.
 
M

Michael J. Hunter [MS]

You have two different options: inspecting the object's TypeName or
checking whether the TypeOf the object Is a particular type.

Public Sub DiscerningType()
Dim list As New Collection

Dim aVariable As New MyClass
Dim anotherVariable As New MyClass
Dim yetAnotherVariable As New MyOtherClass
Dim someOtherVariable As New SomeOtherClass
Dim andYetOneMoreVariable As New MyOtherClass

list.Add aVariable
list.Add anotherVariable
list.Add yetAnotherVariable
list.Add someOtherVariable
list.Add andYetOneMoreVariable

Debug.Print "Using TypeName:"
Dim current As Variant
For Each current In list
Debug.Print TypeName(current)
Next current

Debug.Print
Debug.Print "Using TypeOf...Is:"
For Each current In list
If (TypeOf current Is MyClass) Then
Debug.Print "My Class"
ElseIf (TypeOf current Is MyOtherClass) Then
Debug.Print "My Other Class"
ElseIf (TypeOf current Is SomeOtherClass) Then
Debug.Print "Some Other Class"
Else
Debug.Print "No idea what this is"
End If
Next current
End Sub

This code produces the following output:
Using TypeName:
MyClass
MyClass
MyOtherClass
SomeOtherClass
MyOtherClass

Using TypeOf...Is:
My Class
My Class
My Other Class
Some Other Class
My Other Class


Michael J. Hunter
Microsoft
michhu-at-online-dot-microsoft-dot-com

Developing Microsoft Visio Solutions:
http://msdn.microsoft.com/library/en-us/devref/HTML/DVS_Copyright_1270.asp
Developer Resources for Microsoft Visio:
http://msdn.microsoft.com/library/default.asp?url=/nhp/Default.asp?contentid=28000456

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 

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