C# programatically using a string as object name when instantiating an object

Posted by emk on Stack Overflow See other posts from Stack Overflow or by emk
Published on 2008-11-24T12:16:14Z Indexed on 2010/04/09 19:13 UTC
Read the original article Hit count: 313

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about oop