Bottom line is that you cannot get the variable name at runtime from
Quote:
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
Chris Dunaway wrote:
Quote:
rowe_newsgroups wrote:
Quote:
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.