473,698 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.WriteLi ne(Me.<objectna me>)
End Sub

End Class

EXPECTED OUTPUT:
c1

Thanks!
Ron
Nov 13 '06 #1
7 1545
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.WriteLi ne(Me.<objectna me>)
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.WriteLi ne(Me.<objectna me>)
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 "VariableNa me" 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.WriteLi ne(Me.<objectna me>)
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.WriteLi ne(Me.<objectna me>)
End Sub

End Class
Nov 13 '06 #3
RSH
I understand. Thanks for the clarification.

Ron
"rowe_newsgroup s" <ro********@yah oo.comwrote in message
news:11******** *************@b 28g2000cwb.goog legroups.com...
Basically, if you want the name of the variable you're going to need to
add a property like "VariableNa me" 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.WriteLi ne(Me.<objectna me>)
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.WriteLi ne(Me.<objectna me>)
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 "VariableNa me" 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.WriteLi ne(c1.VariableN ame) 'Will print c2
Console.WriteLi ne(c2.VariableN ame) '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 "VariableNa me" 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.WriteLi ne(c1.VariableN ame) 'Will print c2
Console.WriteLi ne(c2.VariableN ame) '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.Writeli ne 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.goo glegroups.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
22523
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 instance (in this case 'myDog'). Of course it would be better if I could somehow know from within write() that the name of the object instance was 'myDog' without having to pass it as a parameter. //////////////////////////////// function...
106
6440
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 dao.recordset
12
2014
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.., which are suppose to describe the structure of my main TextFile class. Also i have created some custom collection classes, which only take items of the types, they are designed for. Now the problem is that I want my element classes, like...
38
3720
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 objects and how to implement them with c#? I would appreciate any help. Thanks.
8
1857
by: Lüpher Cypher | last post by:
Hi, Suppose we have a hierarchical class structure that looks something like this: Object | +-- Main | +-- Object1
161
7826
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.. (After reading that
15
449
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
6012
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 static inside functions (i.e. local static objects) 5. objects declared at file scope.
2
1877
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 differences between delegates and the use of pointers to objects, I'm nonetheless a little confused by how similar the syntax used for references to methods is to that used for references to objects. Recently at a few different forums I posted a brief...
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7728
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.