dynamic member reference

  • Thread starter Thread starter Sharon
  • Start date Start date
S

Sharon

Hi to all.
How do i reference a class member dynamically?
Somthing like:
byte i = 35;
Button currentButton = this["button" + i.ToString()];
Thanks,
Sharon.
 
Hi,

You can use reflection, it does always works.

If you have either a controls ( either web or win) collection you could
iterate in the collection looking for the name of the control.

In a similar way you could create a hash with the controls that you needs to
have accesibles and use as the key the value you want.


cheers,
 
Sharon,

You would have to use reflection. First, you get the type for your
class. If the type is referenced, then you can do this:

// Get the Type.
Type type = typeof(<containing class>);

Or, you can just call GetType on yourself (or any other object
reference):

// Get the type.
Type type = this.GetType();

Then, you get the name of the field that you want:

// Get the field information.
FieldInfo fieldInfo = type.GetField("button" + i.ToString());

Finally, to get the field, you would call GetValue, passing the instance
of the object you want to get the field for:

// Get the value. The value has to be cast to a Button, since GetValue
returns an object.
Button currentButton = (Button) fieldInfo.GetValue(this);

Hope this helps.
 
That might not always be the case. If one doesn't have permission for
reflection, it won't work.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

You can use reflection, it does always works.

If you have either a controls ( either web or win) collection you could
iterate in the collection looking for the name of the control.

In a similar way you could create a hash with the controls that you needs
to have accesibles and use as the key the value you want.


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Sharon said:
Hi to all.
How do i reference a class member dynamically?
Somthing like:
byte i = 35;
Button currentButton = this["button" + i.ToString()];
Thanks,
Sharon.
 
type.GetField("button" + i.ToString())
returns null.
type.GetField("button1")
also returns null.
Does it have to be a static member?
Shron.


Nicholas Paldino said:
Sharon,

You would have to use reflection. First, you get the type for your
class. If the type is referenced, then you can do this:

// Get the Type.
Type type = typeof(<containing class>);

Or, you can just call GetType on yourself (or any other object
reference):

// Get the type.
Type type = this.GetType();

Then, you get the name of the field that you want:

// Get the field information.
FieldInfo fieldInfo = type.GetField("button" + i.ToString());

Finally, to get the field, you would call GetValue, passing the instance
of the object you want to get the field for:

// Get the value. The value has to be cast to a Button, since GetValue
returns an object.
Button currentButton = (Button) fieldInfo.GetValue(this);

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Sharon said:
Hi to all.
How do i reference a class member dynamically?
Somthing like:
byte i = 35;
Button currentButton = this["button" + i.ToString()];
Thanks,
Sharon.
 
Silly me, reflection cannot see private fields.
Thanks a bunch Nicholas.

Sharon said:
type.GetField("button" + i.ToString())
returns null.
type.GetField("button1")
also returns null.
Does it have to be a static member?
Shron.


message news:[email protected]...
Sharon,

You would have to use reflection. First, you get the type for your
class. If the type is referenced, then you can do this:

// Get the Type.
Type type = typeof(<containing class>);

Or, you can just call GetType on yourself (or any other object
reference):

// Get the type.
Type type = this.GetType();

Then, you get the name of the field that you want:

// Get the field information.
FieldInfo fieldInfo = type.GetField("button" + i.ToString());

Finally, to get the field, you would call GetValue, passing the instance
of the object you want to get the field for:

// Get the value. The value has to be cast to a Button, since GetValue
returns an object.
Button currentButton = (Button) fieldInfo.GetValue(this);

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Sharon said:
Hi to all.
How do i reference a class member dynamically?
Somthing like:
byte i = 35;
Button currentButton = this["button" + i.ToString()];
Thanks,
Sharon.
 
Back
Top