473,388 Members | 1,400 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,388 software developers and data experts.

getting the name of the defined instance variable

Hi

Consider the following :

Myclass myvar=new Myclass ();
is there a way to get the name of the instance of the variable inside the
class ?
so i know in my class that the variables name is myvar ?

Can this be done through reflection ?
Johan



Nov 16 '05 #1
4 3493
Sagaert,

No, you can't. Here is why. Suppose you have a method which relies on
this, called GetVariableName, then what happens when you do this?

Myclass myvar = new Myclass();
Myclass myvar2 = myvar;
myvar.GetVariableName();

The problem is that myvar and myvar2 reference the same thing, so there
are two variables (there can be an unlimited number, or none, really,
because your instance might be ready for a GC).

What are you trying to do?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Sagaert Johan" <RE*************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi

Consider the following :

Myclass myvar=new Myclass ();
is there a way to get the name of the instance of the variable inside the
class ?
so i know in my class that the variables name is myvar ?

Can this be done through reflection ?
Johan


Nov 16 '05 #2
Sagaert Johan <RE*************@hotmail.com> wrote:
Consider the following :

Myclass myvar=new Myclass ();

is there a way to get the name of the instance of the variable inside
the class ?
so i know in my class that the variables name is myvar ?

Can this be done through reflection ?


You can use reflection to find out what the member names of a type are.
However, an object itself doesn't have the name - you could have
multiple variables which all have a reference to the same object, or
none!

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
Sagaert,

No, you can't. Here is why. Suppose you have a method which relies on this, called GetVariableName, then what happens when you do this?

Myclass myvar = new Myclass();
Myclass myvar2 = myvar;
myvar.GetVariableName();

The problem is that myvar and myvar2 reference the same thing, so there are two variables (there can be an unlimited number, or none, really,
because your instance might be ready for a GC).

What are you trying to do? ok
seems its is impossible todo it i will change myclass to work like

MyClass myvar=new MyClass("myvar");

I am writing a class for persistent variables that are read/written in the
registry.
for use like this

string y=myvar.val ; // get the value from registry
or
myvar.val="stored for later use" ; // store it


--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Sagaert Johan" <RE*************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi

Consider the following :

Myclass myvar=new Myclass ();
is there a way to get the name of the instance of the variable inside the class ?
so i know in my class that the variables name is myvar ?

Can this be done through reflection ?
Johan



Nov 16 '05 #4
Sagaert,

If that is the case, then passing the name of the key to read makes the
most sense. It also improves code readability, as you can tell what you are
trying to do from the value passed in (instead of having to know to look at
the variable name).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Sagaert Johan" <RE*************@hotmail.com> wrote in message
news:ez**************@TK2MSFTNGP12.phx.gbl...

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
Sagaert,

No, you can't. Here is why. Suppose you have a method which relies

on
this, called GetVariableName, then what happens when you do this?

Myclass myvar = new Myclass();
Myclass myvar2 = myvar;
myvar.GetVariableName();

The problem is that myvar and myvar2 reference the same thing, so

there
are two variables (there can be an unlimited number, or none, really,
because your instance might be ready for a GC).

What are you trying to do?

ok
seems its is impossible todo it i will change myclass to work like

MyClass myvar=new MyClass("myvar");

I am writing a class for persistent variables that are read/written in the
registry.
for use like this

string y=myvar.val ; // get the value from registry
or
myvar.val="stored for later use" ; // store it


--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Sagaert Johan" <RE*************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
> Hi
>
> Consider the following :
>
> Myclass myvar=new Myclass ();
>
>
> is there a way to get the name of the instance of the variable inside the > class ?
> so i know in my class that the variables name is myvar ?
>
> Can this be done through reflection ?
>
>
> Johan
>
>
>
>
>
>
>



Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Andre Meyer | last post by:
Hi all I have been searching everywhere for this, but have not found a solution, yet. What I need is to create an object that is an instance of a class (NOT a class instance!) of which I only...
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...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
20
by: Shawnk | last post by:
I would like to get the class INSTANCE name (not type name) of an 'object'. I can get the object (l_obj_ref.GetType()) and then get the (l_obj_typ.Name) for the class name. I there any way of...
6
by: Adam Atlas | last post by:
Is it possible for an object, in its __init__ method, to find out if it is being assigned to a variable, and if so, what that variable's name is? I can think of some potentially ugly ways of...
3
by: lakepeir | last post by:
Hello, I'm have defined a combobox on form2 and I'm trying to use that value in form1. I have defined a global value so that form1 can retrieved the modified value as: Public Shared...
41
by: Jim | last post by:
Hi guys, I have an object which represents an "item" in a CMS "component" where an "item" in the most basic form just a field, and a "component" is effectively a table. "item" objects can be...
0
by: rautsmita | last post by:
hello friends , i am using to jdk6 and JAXB2.0, i have geomtry.xsd file i am trying to compile this file using jaxb but i got some error i.e.The particle of the type is not a valid restriction of...
23
by: Hugh Oxford | last post by:
How do I get an object's name? EG. $obj_FOO = new Bar; echo $obj_FOO->getName(); 'obj_FOO'
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...

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.