473,396 Members | 2,010 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,396 software developers and data experts.

dynamic member reference

Hi to all.
How do i reference a class member dynamically?
Somthing like:
byte i = 35;
Button currentButton = this["button" + i.ToString()];
Thanks,
Sharon.
Nov 16 '05 #1
5 1915
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" <ta*******@void.null> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi to all.
How do i reference a class member dynamically?
Somthing like:
byte i = 35;
Button currentButton = this["button" + i.ToString()];
Thanks,
Sharon.

Nov 16 '05 #2
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]
- mv*@spam.guard.caspershouse.com

"Sharon" <ta*******@void.null> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi to all.
How do i reference a class member dynamically?
Somthing like:
byte i = 35;
Button currentButton = this["button" + i.ToString()];
Thanks,
Sharon.

Nov 16 '05 #3
That might not always be the case. If one doesn't have permission for
reflection, it won't work.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:Ok**************@TK2MSFTNGP10.phx.gbl...
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" <ta*******@void.null> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi to all.
How do i reference a class member dynamically?
Somthing like:
byte i = 35;
Button currentButton = this["button" + i.ToString()];
Thanks,
Sharon.


Nov 16 '05 #4
type.GetField("button" + i.ToString())
returns null.
type.GetField("button1")
also returns null.
Does it have to be a static member?
Shron.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ur**************@TK2MSFTNGP10.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Sharon" <ta*******@void.null> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi to all.
How do i reference a class member dynamically?
Somthing like:
byte i = 35;
Button currentButton = this["button" + i.ToString()];
Thanks,
Sharon.


Nov 16 '05 #5
Silly me, reflection cannot see private fields.
Thanks a bunch Nicholas.

"Sharon" <ta*******@void.null> wrote in message
news:uU**************@TK2MSFTNGP12.phx.gbl...
type.GetField("button" + i.ToString())
returns null.
type.GetField("button1")
also returns null.
Does it have to be a static member?
Shron.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:ur**************@TK2MSFTNGP10.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Sharon" <ta*******@void.null> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi to all.
How do i reference a class member dynamically?
Somthing like:
byte i = 35;
Button currentButton = this["button" + i.ToString()];
Thanks,
Sharon.



Nov 16 '05 #6

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

Similar topics

2
by: festiv | last post by:
Hi there, I want to learn how the compiler is implementing the dynamic binding. where can i read about this subject (the hole process). thanks.
4
by: Jason Heyes | last post by:
What can I do to circumvent the performance degradation associated with dynamic allocation and small objects? Thanks.
3
by: deejayquai | last post by:
Hi I've created a crosstab query and displayed it as a sub-report in my main report. This is fine until the data changes and the column names become incorrect. I know I have to create a...
8
by: Eyeawanda Pondicherry | last post by:
I have put some code together that creates an enum dynamically from some database values. The enum can be read perfectly by an application that references the dynamically generated dll. If I...
2
by: WhiskyRomeo | last post by:
Documentation states the setting the URL Behavior property to "Dynamic" forces the web reference to use search the app.config file (actually deployed version of the file) to get the URL. the URL...
7
by: Mike Livenspargar | last post by:
We have an application converted from v1.1 Framework to v2.0. The executable references a class library which in turn has a web reference. The web reference 'URL Behavior' is set to dynamic. We...
1
by: Tarun Mistry | last post by:
Hi all, I am adding dynamic user controls with the simple code as follows: MyPlaceHolder.Controls.Add(LoadControl("MyUserControl.ascx")); I then want to run a member on my user control,...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
14
by: Daniel Lidström | last post by:
Hello! I have just discovered a way to use the private implementation idiom (pimpl), without the overhead of dynamic memory allocation. For those of you who don't know what this is, Wikipedia...
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:
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.