473,379 Members | 1,330 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Getting an objects name from within a class

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
7 1522
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
106
by: xtra | last post by:
Hi Folk I have about 1000 procedures in my project. Many, many of them are along the lines of function myfuntion () as boolean on error goto er '- Dim Dbs as dao.database Dim Rst as...
12
by: Sunny | last post by:
Hi All, I have a serious issue regarding classes scope and visibility. In my application, i have a class name "TextFile", and also a few other classes like "TotalWords", "TotalLines" and etc..,...
38
by: Radi Radichev | last post by:
Hi! I'm making a database application and i heard from a friend that it is more proffecional and easy to do this with bussines objects. Can anyone tell me where i can find more info on bussines...
8
by: Lüpher Cypher | last post by:
Hi, Suppose we have a hierarchical class structure that looks something like this: Object | +-- Main | +-- Object1
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
15
by: TKirahvi | last post by:
Why can't I create Objects in an Array within for/while loop? If I have: Code: $galleries = array(); while( $db->next_record() ) {
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
2
by: BobLewiston | last post by:
The concept of delegates (references to methods) per se is new to me (although I used function pointers in C years ago), and although the literature acknowledges that there are significant...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.