Creating a copy of an existing class object
I have a class which is used to created various items and store them in a static dictionary database. Creating and adding items to the database is working just as intended but I have ran into an issue where I would like to created a copy of the object stored in the dictionary to put into another dictionary but it seems all I am able to do is reference the original object.
This may be silly, it is just something I have been playing around with.
Item Code:
public class Armour : Item
{
#region Declarations
int _ArmourLevel;
int _ArmourValue;
ArmourType _ArmourType;
#endregion
#region Properties
public int Level { get { return _ArmourLevel; } }
public int Defense { get { return _ArmourValue; } }
public ArmourType Type { get { return _ArmourType; } }
#endregion
#region Armour Methods
public Armour (string name, int buy, float weight, RarityTier rarity, int level, int armour, ArmourType type)
{
_ArmourLevel = level;
_ArmourValue = armour;
_ArmourType = type;
int id = DB.ArmourDB.Count > 0 ? DB.ArmourDB.Keys.Max() + 1 : 1;
SetDefinition(name, id, false, 1, buy, weight, rarity);
DB.ArmourDB.Add(id, this);
}
#endregion
}
I create the item like below:
armour1 = new Armour("Helm of sight", 185, 11, RarityTier.Rare, 8, 10, ArmourType.Helmet);
I want to create a copy of the object, the only way I can think of is the following below but all it does is reference the object in the dictionary. Using the 'new' keyword causes an error. I'd like newArmour to become its own instance of the object but I do not know how to achieve this.
Armour newArmour = DB.ArmourDB[1];
I would appreciate any/all help, be it to gain a copy of the object or how to better write such classes ^ ^
Answer by Ermiq · Mar 18, 2021 at 06:23 AM
There's no built-in Clone()
function in C# that could make a deep clone copy of an object.
You can do that manually though. The simplest way is just create a new object and set its fields to the values of the original's:
public class MyClass
{
private int myInt;
private string myString;
public int MyInt => myInt;
public string MyString => myString;
public MyClass(int intArg, string stringArg)
{
myInt = intArg;
myString = stringArg;
}
public MyClass Clone()
{
MyClass clone = new MyClass(myInt, myString);
return clone;
}
}
Usage:
MyClass mc1 = new MyClass(1, "s");
MyClass mc2 = mc1.Clone();
In your case it could be:
public Armour Clone()
{
Armour clone = new Armour (name, buy, weight, rarity, _Level, _ArmourValue, _ArmourType);
return clone;
}
Usage:
Armour newArmour = DB.ArmourDB[1].Clone();
Obviously, you'll need to get name
, buy
, rarity
and such before creating the new clone
object within the Clone()
method.
Your answer
Follow this Question
Related Questions
Information about Classes and Instantiate Objects C# 2 Answers
How to modify a 2d array [,] of a class? 1 Answer
Custom class, Null Reference Exception 4 Answers
Unity MonoBehaviour Objects vs Game Logic 1 Answer
Instantiating an DynamicObject Class 0 Answers