简体   繁体   中英

c# how to add class to a list from an array of classes

I have a class to define for example a ship, then I have an array of the class with some default parameters But later on I want to be able to add to a list of the class using one of the pre defined examples. I don't know how to add the predefined class to the list at the same time creating a copy of it and not a pointer to the original array. Here is the code to hopefully make it all clear.

   public enum ShipType
    {
      Capital,
      Cruiser,
      Frigate,
      Fighter,
      Colony,
      NumShipTypes
    }
    public class Ship
    {
      public Vector3 ShipPosition;
      public ShipType Type;
      public string DesignName;
      public Ship(ShipType type,string designame)
      {
        Type = type;
        DesignName = designame;
        ShipPosition = Vector3.zero;
      }
    }

Then my array of default types:-

public Ship[] AllShips = new Ship[(int)ShipType.NumShipTypes]
  { 
    new Ship(ShipType.Capital,"MassiveOne"),
    new Ship(ShipType.Cruiser,"MediumOne"),
    new Ship(ShipType.Frigate,"SmallerOne"),
    new Ship(ShipType.Fighter,"TinyOne"),
    new Ship(ShipType.Colony,"Mayflower")
  };

Now I create a list and heres where I want to add a copy of one of the above:-

public List<Ship> FleetShips = new List<Ship>(); 

FleetShips.Add(AllShips[(int)ShipType.Fighter]); 

But this seems to just add a reference or pointer to the one in the array. What I need is something like this, but that actually works:-

FleetShips.Add(new AllShips[(int)ShipType.Fighter]()); 

but this give me "is a field but is used like a type" error

Any ideas?

You can make your class implement the ICloneable interface and implement your own Clone() method.

Then when you want to add it to your collection you can do this

FleetShips.Add((Ship)AllShips[(int)ShipType.Fighter].Clone());

Another possibility is to add a copy-constructor to your Ship-class.

public class Ship
{
  public Vector3 ShipPosition;
  public ShipType Type;
  public string DesignName;
  public Ship(ShipType type,string designame)
  {
    Type = type;
    DesignName = designame;
    ShipPosition = Vector3.zero;
  }
  //copy-constructor
  public Ship(Ship other)
  {
    Type = other.Type;
    DesignName = other.DesignName;
    ShipPosition = Vector3.zero;
  }
}

when you want add a copy of the ship you do

FleetShips.Add(new Ship(AllShips[(int)ShipType.Fighter]));

Implement ICloneable.

   public class Ship: ICloneable
    {
        public Vector3 ShipPosition;
        public ShipType Type;
        public string DesignName;
        public Ship(ShipType type, string designame)
        {
            Type = type;
            DesignName = designame;
            ShipPosition = Vector3.zero;
        }


        public object Clone()
        {
           return this.MemberwiseClone();
        }
    }

Add

FleetShips.Add(AllShips[(int)ShipType.Fighter].Clone() as Ship); 

Edit: Could still be a problem depending on what Vector3 is though

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM