locked
Store refrence to class?

    Question

  • all my bullets are stored as classes, and id like to have the type of ammo stored in a variable for use like this:

    Bullets.Add(new Ammo(Location, RotationAngle));

     where ammo is the name of the variable not the name of the bullet class

    so basically i want a variable that holds the type of ammo but the types of ammo are all classes

    Friday, March 6, 2015 3:13 PM

Answers

  • I don't know if I understand what you mean here but a variable that holds an object of type Ammo is just:

    Ammo myVariable = new Ammo(Location, RotationAngle);


    If you want to store several Ammo objects in the same variable you could use a List<Ammo>:

    List<Ammo> Bullets = new List<Ammo>();
    Bullets.Add(new Ammo(Location, RotationAngle));


    If you have different types of Ammo or Bullet classes you should specify the base class as the type argument:

    List<AmmoBase> Bullets = new List<AmmoBase>();
    Bullets.Add(new Ammo(Location, RotationAngle));
    
    public class AmmoBase
    {
    }
    
    public class Ammo : AmmoBase
    {
    }
    


    If you want to store the Ammo type in a variable yo do it like this:

    Type ammoType = typeof(Ammo);
    

    Please be a lot more specific about yout issue if you want anyone to be able to help you any further.

    Please also remember to close your threads by marking helpful posts as answer and please start a new thread if you have a new question

    • Marked as answer by Riley19280 Monday, March 9, 2015 2:38 AM
    Friday, March 6, 2015 4:10 PM

All replies

  • I don't know if I understand what you mean here but a variable that holds an object of type Ammo is just:

    Ammo myVariable = new Ammo(Location, RotationAngle);


    If you want to store several Ammo objects in the same variable you could use a List<Ammo>:

    List<Ammo> Bullets = new List<Ammo>();
    Bullets.Add(new Ammo(Location, RotationAngle));


    If you have different types of Ammo or Bullet classes you should specify the base class as the type argument:

    List<AmmoBase> Bullets = new List<AmmoBase>();
    Bullets.Add(new Ammo(Location, RotationAngle));
    
    public class AmmoBase
    {
    }
    
    public class Ammo : AmmoBase
    {
    }
    


    If you want to store the Ammo type in a variable yo do it like this:

    Type ammoType = typeof(Ammo);
    

    Please be a lot more specific about yout issue if you want anyone to be able to help you any further.

    Please also remember to close your threads by marking helpful posts as answer and please start a new thread if you have a new question

    • Marked as answer by Riley19280 Monday, March 9, 2015 2:38 AM
    Friday, March 6, 2015 4:10 PM
  • Sorry for not being specific, but the "type" thing was what i was looking for! Thanks!
    Monday, March 9, 2015 2:40 AM