Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB6 Runtime Type Retrieval

Tags:

runtime

vb6

How can you obtain the Type (the name as a string is sufficient) of an Object in VB6 at runtime?

i.e. something like:

If Typeof(foobar) = "CommandButton" Then ...

/EDIT: to clarify, I need to check on Dynamically Typed objects. An example:

Dim y As Object 

Set y = CreateObject("SomeType")

Debug.Print( <The type name of> y)

Where the output would be "CommandButton"

like image 835
DAC Avatar asked Sep 09 '08 15:09

DAC


2 Answers

I think what you are looking for is TypeName rather than TypeOf.

If TypeName(foobar) = "CommandButton" Then
   DoSomething
End If

Edit: What do you mean Dynamic Objects? Do you mean objects created with CreateObject(""), cause that should still work.

Edit:

Private Sub Command1_Click()
    Dim oObject As Object
    Set oObject = CreateObject("Scripting.FileSystemObject")
    Debug.Print "Object Type: " & TypeName(oObject)
End Sub

Outputs

Object Type: FileSystemObject

like image 107
Kris Erickson Avatar answered Oct 26 '22 12:10

Kris Erickson


TypeName is what you want... Here is some example output:

VB6 Code:

Private Sub cmdCommand1_Click()
Dim a As Variant
Dim b As Variant
Dim c As Object
Dim d As Object
Dim e As Boolean

a = ""
b = 3
Set c = Me.cmdCommand1
Set d = CreateObject("Project1.Class1")
e = False

Debug.Print TypeName(a)
Debug.Print TypeName(b)
Debug.Print TypeName(c)
Debug.Print TypeName(d)
Debug.Print TypeName(e)
End Sub

Results:

String
Integer
CommandButton
Class1
Boolean
like image 35
sharvell Avatar answered Oct 26 '22 12:10

sharvell