1

I was wondering if it is possible to instantiate a class with a string variable as its name in C#. I don't know how else to explain it other than this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
public class Product
{
    string name;
    decimal cost;

    Product(string _name, decimal _cost)
    {
        name = _name;
        cost = _cost;
    }
}
class Program
{
    static void Main(string[] args)
    {
        string nameForInstantiatedClass = "DellComputer";
        Product nameForInstantiatedClass = new Product("Inspiron", 399.99m);
    }
}
}

Is it possible to do something like this or to the same effect, using a string to declare the name of an instantiated class or is it just impossible to do? Any help is appreciated.

5
  • 1
    you could put it in dictionary. what are you planning to do? Commented Sep 15, 2015 at 23:45
  • Are you trying to create your object late bound? Such like in vb CreateObject("myobject.myobject"). You would use reflection to do this. Commented Sep 15, 2015 at 23:49
  • Are you trying to do the equivalent of Product DellComputer = new Product(... with "DellComputer" being grabbed from a string? No, it's not possible. Commented Sep 15, 2015 at 23:50
  • Please look at php's variable variables. Is this what you're trying to make happen? (I know this question is about C#. Its just that PHP is the only language I can think of that has that anti-pattern) Commented Sep 15, 2015 at 23:52
  • As @Blorgbeard said, if you are trying to name the variable so that later you can do DellComputer.ToString()..., that's not possible. If you are trying to create an instance of a subclass of Product called DellComputer (that you have already defined), you would do that with reflection. Commented Sep 15, 2015 at 23:57

2 Answers 2

1

One thing that comes to my mind is to use a

 var products = new Dictionary<string,Product>();

and then you can save / retrieve items like this

products.Add(nameForInstantiatedClass, ProductObject);
Product dellComp = products[nameForInstantiatedClass]
Sign up to request clarification or add additional context in comments.

2 Comments

So this would instantiate a new instance of the class? I don't know much about Dictionaries and the reason I would like to do this is so I could instantiate new classes based on user input.
Every entry in the Dictionary has a Key (eg DellComp) and a Value. In Dictionary<string,Product> means that every key is going to be of type string and every value of type Product. In the dictionary you add Product items which are usually already instatiated but you can do dictionary.Add("dell",new Product());
0

I don't know why you want to do that. Is there an other logic for you? You could put the instance in a List or Dictionary like :

Dictionary<String, Product> dict = new Dictionary()
dict.add("DellComputer", new Product("",399.99m);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.