Duplicate Object and working with Duplicate without changing Original

In order to change one instance without changing the other you need to clone the actual values of this instance and not the reference.
The pattern used in .Net is to implement ICloneable.
So your code would look like this:

public class ItemVO: ICloneable
  {
    public ItemVO()
    {
        ItemId = ""; 
        ItemCategory = ""; 
    }

    public string ItemId { get; set; }
    public string ItemCategory { get; set; }

    public object Clone()
    {
        return new ItemVO
        {
            ItemId = this.ItemId,
            ItemCategory = this.ItemCategory
        }; 
    }
 }

Now notice that you need an explicit cast when using Clone() (or you can make your own that returns ItemVO).

duplicateItemVO = (ItemVO) originalItemVO.Clone(); 

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)