473,493 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

expression evaluation

If I have a string variable that contains the name of a property, how can I
evaluate that string in code to be used as a property?

For example:

public struct myData
{
public string first_item;
public string next_item;
}

myData x;
string myItem = "first_item";

x.myItem <----- This is where I want the myItem evaluated so it understands
this is a property of x.

Thanks!

Nov 17 '05 #1
2 1358
David wrote:
If I have a string variable that contains the name of a property, how can I
evaluate that string in code to be used as a property?

For example:

public struct myData
{
public string first_item;
public string next_item;
}

myData x;
string myItem = "first_item";

x.myItem <----- This is where I want the myItem evaluated so it understands
this is a property of x.


Reflection. typeof(myData).GetProperty(myItem)

--

www.midnightbeach.com
Nov 17 '05 #2
I wonder why you need this, perhaps there is another way with no
performace penalty.

if you need, use Reflection, example:
//-------------------------------------
public static void Main()
{
myData x;
x.first_item = "{test first_item}";
x.next_item = "{test next_item}";

string myItem = "first_item";

FieldInfo myFieldInfo;
Type myType = typeof(myData);
// Get the type and fields of FieldInfoClass.
myFieldInfo = myType.GetField(myItem);
Console.WriteLine(myFieldInfo.FieldType.ToString() );
Console.WriteLine(myFieldInfo.Name.ToString());
Console.WriteLine(myFieldInfo.GetValue(x).ToString ());
}

public struct myData
{
public string first_item;
public string next_item;
}

Nov 17 '05 #3

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

Similar topics

1
2681
by: Simon Wigzell | last post by:
A client wants have acess to an online databases records controlled by group strings and evaluation strings e.g., each use would have in his client record a group string like this: And each...
4
2556
by: Frank Wallingford | last post by:
Note: For those with instant reactions, this is NOT the common "why is i = i++ not defined?" question. Please read on. I came across an interesting question when talking with my colleagues....
8
1587
by: manan.kathuria | last post by:
hi all , the expression in question is ++i&&++j||++k most sources say that since the result of the || operation is decided by the LHS itself , the right side is not computed my point of...
2
4266
by: Jeffrey Ganping Chen | last post by:
I'm trying to build a generic runtime C# expression evaluation engine to allow the user to evaluate any C# code blocks at runtime within the application's context. For example, obj4.p4 =...
8
1742
by: Brian Blais | last post by:
Hello, I have a string input from the user, and want to parse it to a number, and would like to know how to do it. I would like to be able to accept arithmetic operations, like: '5+5'...
8
2181
by: junky_fellow | last post by:
Hi, Sorry, for asking similar questions again and again. 1) I want to know how should we reslove the ambiguities in a c expression ? Should we use precedence table as mentioned in K&R book ...
21
2371
by: Steven T. Hatton | last post by:
I'm trying to improve my formal understanding of C++. One significant part of that effort involves clarifying my understanding of the vocabulary used to describe the language. This is from the...
2
401
by: Tim Johnson | last post by:
I'm having trouble understanding this part of the expression evaluation rules ('6.5 Expressions', second item, C99 spec; the C++ wording is presumably identical): What precisely does the...
12
1589
by: Neroku | last post by:
I don't know if the following expression is UB: i=2; x = (i=3) * i; Since in C, evaluation order is unspecified, this expression is 'at least' unspecified, since we don't know which operand...
32
3253
by: silpau | last post by:
hi, i am a bit confused on expression evaluation order in expressions involving unary increment.decrement operators along with binary operators. For example in the following expression x...
0
7119
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
6989
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
7157
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
7195
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...
1
6873
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
5453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1400
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
644
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.