Getting Name of Class Instance (VB 6 & VB 2005)

M

Michael D. Ober

Is there a way in VB 6 (and VB 2005) to get the instance name from a class.
For example

class MyClass
'some methods and properties
public property get VarName() as string
VarName = somefunction(me)
end property

end class


dim myVar as MyClass
Set myVar = new MyClass
debug.print myVar.VarName

The output would be "myVar"

The TypeName() function returns the name of the class itself, in this case
"MyClass". What I'm looking for is a class internal method that will return
the name of the instance variable.

Thanks,
Mike Ober.
 
M

Mike D Sutton

Is there a way in VB 6 (and VB 2005) to get the instance name from a
class.
For example
The TypeName() function returns the name of the class itself, in this case
"MyClass". What I'm looking for is a class internal method that will return
the name of the instance variable.

There is no function for this, however you could easily add a new property
of your object that would be set when the object is created and could be
queried later.
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: (e-mail address removed)
WWW: Http://EDais.mvps.org/
 
K

Karl E. Peterson

Michael said:
Is there a way in VB 6 (and VB 2005) to get the instance name from a
class.

No, the closest you can come is ObjPtr(Me). The concept of instance name is kind of
odd, if you think about it. Consider that a single instance reference may be held in
multiple variables and/or collections.
 
K

Karl E. Peterson

Mike said:
There is no function for this, however you could easily add a new
property of your object that would be set when the object is created
and could be queried later.

Yeah, good idea -- I often add a .Tag property to my objects, for this sort of use.
 
M

Mark Hurd

Michael said:
Is there a way in VB 6 (and VB 2005) to get the instance name from a
class. For example

class MyClass
'some methods and properties
public property get VarName() as string
VarName = somefunction(me)
end property

end class


dim myVar as MyClass
Set myVar = new MyClass
debug.print myVar.VarName

The output would be "myVar"

What would you have it show if the code above continued:

Dim myVar2 as MyClass
Set myVar2 = myVar
Debug.Print myVar.VarName; ", "; myVar2.VarName

As others have said, provide a (write once) property yourself.

[In VB.NET you can set it in the constructor:

Set myVar = New MyClass("myVar")
]
 

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