This is a contrived example, but lets say I have declared objects:
CustomObj fooObj;
CustomObj barObj;
CustomObj bazObj;
And I have an string array:
string[] stringarray = new string[] {"foo","bar","baz"};
How can I programatically access and instantiate those objects using the string array, iterating using something like a foreach:
foreach (string i in stringarray) {
`i`Obj = new CustomObj(i);
}
Hope the idea I'm trying to get across is clear. Is this possible in C#?
Thanks in advance.
@jotte: Thanks alot for that function. I used it and it works! Except that you need to change container.ID by container.Name
Then you just need to use something like (this example is for checkbox, but any type of variable could work):
string Test = "cbACn" + i.ToString(); CheckBox cbTest = (CheckBox)FindControl(Test, gbACSCAT); if (cbTest != null) { cbTest.Checked = true; }
One option is the totally dynamic route, as per this article, where you specify a code block in a string and then compile/run it from within your program