Quantcast
Channel: Questions in topic: "class instance"
Viewing all articles
Browse latest Browse all 131

Creating a copy of an existing class object

$
0
0
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 ^ ^

Viewing all articles
Browse latest Browse all 131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>