-1

I don't know either scenario is possible or not but let me ask you

scenario 1

bool blObject_1=false;
bool blObject_2=false;

somehow access these objects like

int irParam1=1;
int irParam2=2;

blObject_irParam1=true; // this will reference blObject_1
blObject_irParam2=true; // this will reference blObject_2

ok scenario 2

compose a dictionary that will hold references of objects so i can call via key and modify such as

Dictionary myDic<string,bool> = new Dictionary<string,bool>();
bool blObject_1=false;
bool blObject_2=false;
myDic.add("object1",blObject_1);
myDic.add("object2",blObject_2);

myDic["object1"]=true; // this will actually change blObject_1

c# 4 or c# 4.5 possible ?

i want to access a variable dynamically somehow

thank you

Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
  • Please rephrase your question. It is difficult to understand what is being asked. – Simon Whitehead Jan 23 '13 at 00:11
  • @SimonWhitehead this is best i can do. i think it is pretty clear. – Furkan Gözükara Jan 23 '13 at 00:13
  • No, your second scenario doesn't work as you intend. You need to check the difference between value types and reference types. http://www.albahari.com/valuevsreftypes.aspx – Steve Jan 23 '13 at 00:15
  • @Steve i know. i wonder somehow that kind of thing possible. so compose a references thing and access objects dynamically. – Furkan Gözükara Jan 23 '13 at 00:16
  • or dynamically access objects that you know its name and type. – Furkan Gözükara Jan 23 '13 at 00:21
  • where is this declared..? `blObject_irParam1` your variable names are a bit odd not to mention it's hard to determine what you are asking here.. try to rephrase your question.. What does this actually mean `i want to access an object dynamically somehow` – MethodMan Jan 23 '13 at 00:24
  • @DJKRAZE think it like string operation so it composes blObject_1 and access it. – Furkan Gözükara Jan 23 '13 at 00:25
  • I ~think~ the goal here is to make a reference to a value type - something like what you can do with pointers or even references in C++ . Is this correct? – Reed Copsey Jan 23 '13 at 00:26
  • are you talking about `blObject_` + some integer / Index position..? – MethodMan Jan 23 '13 at 00:27
  • @DJKRAZE do you know how to access an object with its defined name ? like in javascript you will call access an object with its name as string. – Furkan Gözükara Jan 23 '13 at 00:28
  • why not make these and enum int irParam1=1; int irParam2=2; then you could get at it based on the enum name – MethodMan Jan 23 '13 at 00:29
  • it is that way right now. imagine that i have 10 params with that naming. and i want to modify each of them in a for loop with single line. you got the idea. – Furkan Gözükara Jan 23 '13 at 00:30
  • no it's not that way right now.. that's not an `enum` ..but you could make properties of that and access those properties or should I say get Property Name via Reflection is that what you are wanting to do..? – MethodMan Jan 23 '13 at 00:31
  • well i really can not make anything else that would require extra work right now. the system is currently poorly designed so i am looking for quick ways to solve if possible. looking for a way that i can access integer or string or bool variables via their names as string. – Furkan Gözükara Jan 23 '13 at 00:33
  • Look at this posting and it will explain how to use Expression tree to do what I think you are trying to do.. also I would suggest working with whom ever on your end to clean up the poor design remember the old saying `Garbage in Equals Garbage Out!` http://stackoverflow.com/questions/2566101/how-to-get-variable-name-using-reflection – MethodMan Jan 23 '13 at 00:36
  • oh i see. so seems like i have no way to achieve what i want :( – Furkan Gözükara Jan 23 '13 at 00:37
  • I have sent you a link so you will need to understand the code there here is another link as well http://stackoverflow.com/questions/716399/c-sharp-how-do-you-get-a-variables-name-as-it-was-physically-typed-in-its-dec Good luck – MethodMan Jan 23 '13 at 00:38

1 Answers1

1

There is no way to make a dynamic reference directly to a value type.

The typically approach here is to wrap the value type (ie: the bool) within a class, and store the class. You can then access the class, and change it's members, from any location.

This allows you to have "shared" state that can change from multiple places.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373