C# APP - Create Multiple Instances of a class a runtime

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jxh00u
    New Member
    • Mar 2008
    • 1

    C# APP - Create Multiple Instances of a class a runtime

    Hi guys,

    I am new to C# and a have a question around class instances.

    I have a class, Pizza. To create an instance I would use:

    Code:
    Pizza pizza1 = new Pizza("Cheese");
    Lets suppose I wanted to create a new instance of a pizza when a customer makes an order at runtime.

    How can I create a Pizza instance at runtime without create the instances first (because i dont know how many pizzas would be ordered)?

    Ideally i would like the instances the take a name I supply at runtime (e.g from a string)
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Well whenever the user orders a pizza say, you could create another instance of it in whatever code runs when they order a pizza.
    A System.Collecti ons object could be used to keep/hold (think an Array only you can keep adding to it with a .Add(myobject)) all the new instances you create.

    There is also System.Reflecti on which would create an instance of an object from a string. I don't know much about it, so you would have to check msdn about it.

    Comment

    Working...