Getting an objects name from within a class

R

RSH

This is probably a very simple question but...

How do I get the instantiated name of an object from within the class?

Example:


Sub main
Dim c1 as new TestClass()
c1.PrintName()
end sub

Public Class TestClass

Public Sub PrintName()
Console.WriteLine(Me.<objectname>)
End Sub

End Class

EXPECTED OUTPUT:
c1

Thanks!
Ron
 
C

Chris Dunaway

RSH said:
This is probably a very simple question but...

How do I get the instantiated name of an object from within the class?

Example:


Sub main
Dim c1 as new TestClass()
c1.PrintName()
end sub

Public Class TestClass

Public Sub PrintName()
Console.WriteLine(Me.<objectname>)
End Sub

End Class

EXPECTED OUTPUT:
c1

What would your expected output be in the following case? Since Me
refers to the object instance and both variables (c1 and c2) point to
the same instance, how would the object know which name to print?

In short, you can't do this because c1 and c2 are the names of the
*variables* that point to the object. But these names are only for the
benefit of the developer when reading code. When the code is compiled,
the names are gone.

Sub main
Dim c1 As New TestClass()
Dim c2 As TestClass = c1
c1.PrintName()
c2.PrintName()
End Sub

Public Class TestClass

Public Sub PrintName()
Console.WriteLine(Me.<objectname>)
End Sub

End Class
 
R

rowe_newsgroups

Basically, if you want the name of the variable you're going to need to
add a property like "VariableName" to your class and assign it a value.
Personally, I would use a readonly property/variable set and pass the
variable name in the class's constructor and assign it there. By the
way, why do you need to know the variable name anyways?

Thanks,

Seth Rowe
 
C

Chris Dunaway

rowe_newsgroups said:
Basically, if you want the name of the variable you're going to need to
add a property like "VariableName" to your class and assign it a value.

But what if you have more than one variable that points to the object?
What will get assigned to this property?

Dim c1 As New MyClass

c1.VariableName = "c1"

Dim c2 As MyClass = c1

c2.VariableName = "c2"

Console.WriteLine(c1.VariableName) 'Will print c2
Console.WriteLine(c2.VariableName) 'Will print c2

Bottom line is that you cannot get the variable name at runtime from
the object instance.
 
R

rowe_newsgroups

Bottom line is that you cannot get the variable name at runtime from
the object instance.

Yeah I know, I was just trying to offer a possible workaround (albeit
with it's own limitations as you have pointed out) and not just dismiss
it as impossible to do. Without being given the reasons why the OP
needs to return the variable name I can't offer a better solution to
his problem.

Also, the code I meant in my post was as follows:

Private Readonly m_VariableName as string

Public ReadOnly Property VariableName() As String
Get
Return m_VariableName
End Get
End Property

Sub New(byval variableName as string)
m_VariableName = variableName
End Sub

But as you know, if you assigned c2 as c1 then it would return "c1" in
either case. In short the OP wouldn't be able to use "c2 = c1" if he
wanted to return the correct variable name. And depending on what he
needs it for this may not be cause any problems.

Thanks,

Seth Rowe
 
C

Chris Dunaway

rowe_newsgroups said:
it as impossible to do. Without being given the reasons why the OP
needs to return the variable name I can't offer a better solution to
his problem.

You're right, if the OP would tell why he needs this, we can probably
suggest a better alternative for what he wants to do.
 
R

RSH

I dont really have a concrete reason for doing this, other than I was
testing.

I was just having the object identiy itself in a Console.Writeline from
within a function in the class.

I ultimately just made a property which took care of the issue.

thanks,
Ron
 

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