Home All Groups Group Topic Archive Search About

Getting an objects name from within a class

Author
13 Nov 2006 5:48 PM
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

Author
13 Nov 2006 6:29 PM
Chris Dunaway
RSH wrote:
Show quoteHide quote
> 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
Author
13 Nov 2006 6:37 PM
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


Chris Dunaway wrote:
Show quoteHide quote
> RSH wrote:
> > 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
Author
13 Nov 2006 7:08 PM
RSH
I understand.  Thanks for the clarification.

Ron


Show quoteHide quote
"rowe_newsgroups" <rowe_em***@yahoo.com> wrote in message
news:1163443055.916586.49640@b28g2000cwb.googlegroups.com...
> 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
>
>
> Chris Dunaway wrote:
>> RSH wrote:
>> > 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
>
Author
13 Nov 2006 7:20 PM
Chris Dunaway
rowe_newsgroups wrote:
> 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.
Author
13 Nov 2006 7:45 PM
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


Chris Dunaway wrote:
Show quoteHide quote
> rowe_newsgroups wrote:
> > 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.
Author
13 Nov 2006 9:25 PM
Chris Dunaway
rowe_newsgroups wrote:

> 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.
Author
13 Nov 2006 10:09 PM
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


Show quoteHide quote
"Chris Dunaway" <dunaw***@gmail.com> wrote in message
news:1163453147.889680.289420@k70g2000cwa.googlegroups.com...
> rowe_newsgroups wrote:
>
>> 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.
>