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.
Product DellComputer = new Product(...with "DellComputer" being grabbed from a string? No, it's not possible.DellComputer.ToString()..., that's not possible. If you are trying to create an instance of a subclass ofProductcalledDellComputer(that you have already defined), you would do that with reflection.