Instance Name and Reflection

S

Scott Eguires

Is there a way to determine the name of a class instance
using reflection or by some other means? For example
suppose a delegate is fired, I would like to know the name
of the Instance of the suscriber class whose method is
being called:

Say I have two suscriber classes:

dim clsSub1 as CSuscriber
dim clsSub2 as CSuscriber

Then say in the base CSuscriber class I have a method
defined:

Public Sub FireEvent()
' I WANT TO DETERMINE THE NAME OF THE INSTANCE
' Class ASSOCIATED WITH THIS
' i.e,.. If the Instance class is clsSub1 I
' want to output "clsSub1"
End Sub

Thanks,

Scott Eguires
 
H

Herfried K. Wagner [MVP]

* "Scott Eguires said:
Is there a way to determine the name of a class instance
using reflection or by some other means? For example
suppose a delegate is fired, I would like to know the name
of the Instance of the suscriber class whose method is
being called:

Say I have two suscriber classes:

dim clsSub1 as CSuscriber
dim clsSub2 as CSuscriber

Then say in the base CSuscriber class I have a method
defined:

Public Sub FireEvent()
' I WANT TO DETERMINE THE NAME OF THE INSTANCE
' Class ASSOCIATED WITH THIS
' i.e,.. If the Instance class is clsSub1 I
' want to output "clsSub1"
End Sub

Why not extend the subscriber class with a 'Name' property or an
automatically generated, unique ID?
 
L

Lifeng Lu

Herfried is right.

There is no way to do that, unless you extend the class, and set the name
by yourself.

Remember clasSub1 is the name of a variable which reference the object. You
can have multi variables reference to the same instance. And the instance
never knows who refers to itself.
 

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