By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,092 Members | 1,650 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,092 IT Pros & Developers. It's quick & easy.

Getting an objects name from within a class

P: n/a
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
Nov 13 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

Nov 13 '06 #2

P: n/a
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
Nov 13 '06 #3

P: n/a
RSH
I understand. Thanks for the clarification.

Ron
"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.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

Nov 13 '06 #4

P: n/a
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.

Nov 13 '06 #5

P: n/a
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:
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.
Nov 13 '06 #6

P: n/a
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.

Nov 13 '06 #7

P: n/a
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
"Chris Dunaway" <du******@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.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.

Nov 13 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.