472,353 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Is it possible to refer to a member programatically (using a variable)?

Hi,
I'm new to C# and I'd like to be able to reference a field of an
object using a variable instead of literal text.

In the PHP scripting language, you can create a variable:
$var = "name";
and another variable:
$name = "Epson";

Then you can refer to $name, or $$var. They will equal the same thing.

Is there similar functionality in C#?

The reason:
I have a loop inside a function which accepts two parameters and tests
for equality - it tests if an objects property with the name of the
first parameter is equal to the second parameter. This is so I don't
have to hard code all possible properties in advance, or update it as
new properties are added.

string myprop = "Branch"; <-- I'd like to use this instead of the
literal text below:

foreach (string key in keys)
{
Product prod = (Product)collection[key];
if (prod.Branch != val)
{
collection.Remove(key);
}
}

Thanks,
Epson
Nov 15 '05 #1
1 1967
Yes, this can be accomplished by using Reflection!

Look at the following code; it shows how to put a value in a dynamically
selected property of a textbox.

string propertyToSet = "Text";
System.Reflection.PropertyInfo pi;
pi = textBox1.GetType().GetProperty(propertyToSet);
pi.SetValue(textBox1,"Dynamically set!!", new object[] {});

First I declare a PropertyInfo variable (pi), and then I get an instance
using the GetProperty method of the Type class (which I got using the
GetType method of my TextBox). If I want to set the value, I use the
SetValue method of the pi object. The first parameter is the object for
which you want to set the value, the second is the actual value and the
third one is the index; which is in this case an empty array. If you want to
retrieve values instead of setting them, use the SetValue method. The same
can be done with methods and functions, then you have to use a MemberInfo
instead of a PropertyInfo object.

Let me know if you need more info!

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
"Epson Barnett" <go****@grider.org> wrote in message
news:42*************************@posting.google.co m...
Hi,
I'm new to C# and I'd like to be able to reference a field of an
object using a variable instead of literal text.

In the PHP scripting language, you can create a variable:
$var = "name";
and another variable:
$name = "Epson";

Then you can refer to $name, or $$var. They will equal the same thing.

Is there similar functionality in C#?

The reason:
I have a loop inside a function which accepts two parameters and tests
for equality - it tests if an objects property with the name of the
first parameter is equal to the second parameter. This is so I don't
have to hard code all possible properties in advance, or update it as
new properties are added.

string myprop = "Branch"; <-- I'd like to use this instead of the
literal text below:

foreach (string key in keys)
{
Product prod = (Product)collection[key];
if (prod.Branch != val)
{
collection.Remove(key);
}
}

Thanks,
Epson

Nov 15 '05 #2

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

Similar topics

11
by: Brent Ritchie | last post by:
Hello all, I have been using C# in my programming class and I have grown quite fond of C# properties. Having a method act like a variable that I...
5
by: Cyril Gupta | last post by:
Hello, I have a class inside another class. The Scenario is like Car->Engine, where Car is a class with a set of properties and methods and...
14
by: Dave Booker | last post by:
It looks like the language is trying to prevent me from doing this sort of thing. Nevertheless, the following compiles, and I'd like to know why it...
0
by: Larry Lard | last post by:
This came out of a thread explaining to "BK" about error BC42025 ("Access of shared member through an instance; qualifying expression will not be...
6
by: Hubert Fritz | last post by:
Hello, I fear I want to have something which is not possible in C++. How is it possible to define a base class so that the derived class is...
5
by: lovecreatesbea... | last post by:
Does the expression *(int *)&s1 below inside the printf() statement guarantee to refer to the first member of the structure variable s1? I've tried...
3
by: Goran | last post by:
Hi @ all! Is it possible to overload a member variable? Example: class ClassA_t {
12
by: Angus | last post by:
Hello I have a class I am using which raises an exception in its constructor if certain things aren't in place. I can easily create the...
2
by: Rick | last post by:
First of all, is there a group more specific to WPF? I have a WPF form and I want to bind to a local member variable from the xaml. the code...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

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.