getting the name of the defined instance variable

S

Sagaert Johan

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
 
N

Nicholas Paldino [.NET/C# MVP]

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?
 
J

Jon Skeet [C# MVP]

Sagaert Johan said:
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!
 
S

Sagaert Johan

Nicholas Paldino said:
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]
- (e-mail address removed)

Sagaert Johan said:
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
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

Sagaert Johan said:
in
message news:%[email protected]...
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]
- (e-mail address removed)

Sagaert Johan said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top