pepperjack
pepperjack

Reputation: 663

Confusion about arrays

I am trying to copy a part of an array to a different location. Here is the declaration.

 public ObjectBasicFeatures[] ALLOBJECTS = new ObjectBasicFeatures[150];

When I do this,

ALLOBJECTS[1]= ALLOBJECTS[0];

Any changes made to either one cause a change in the other one.

From what I understand it is setting the pointers to the same address (Which is 'copying' it, but not what I want).

How can I copy the memory stored at pointer ALLOBJECTS[0] to ALLOBJECTS[1]?

Things tried:

Upvotes: 2

Views: 105

Answers (3)

Greatran
Greatran

Reputation: 190

ALLOBJECTS[1]= new ObjectBasicFeatures {
      PropertyName1=ALLOBJECTS[0].PropertyName1
      PropertyName2=ALLOBJECTS[0].PropertyName2

}

Hope this helps.

If your Class ObjectBasicFeatures has complex properties you should consider deep copy function

Upvotes: 0

Vineet Kumar
Vineet Kumar

Reputation: 176

To achieve this you need to create a constructor which takes input as its object and copies its values. But here is a catch. You need to do the same for all the classes you refer in ObjectBasicFeatures class and so on till the leaf node. Below is a piece of code I tested with.

Please no the value of member (direct member of class) does not reflect in other(copied) element but the value of level2.member is updated in both the objects when you change it in one object

class Program
{
    static void Main(string[] args)
    {
        ObjectBasicFeatures[] ALLOBJECTS = new ObjectBasicFeatures[3];
        ALLOBJECTS[0] = new ObjectBasicFeatures("zero");
        ALLOBJECTS[1] = new ObjectBasicFeatures("one");
        ALLOBJECTS[2] = new ObjectBasicFeatures("two");
        ALLOBJECTS[1] = new ObjectBasicFeatures(ALLOBJECTS[0]);
        ALLOBJECTS[0].member = "Updated Value";
        ALLOBJECTS[0].level2Member.member = "Updated Level 2 Value";
        Console.WriteLine("At index 0 : " + ALLOBJECTS[0].member + ", Level2 : " + ALLOBJECTS[0].level2Member.member);
        Console.WriteLine("At index 1 : " + ALLOBJECTS[1].member + ", Level2 : " + ALLOBJECTS[1].level2Member.member);
        Console.ReadKey();
    }
}

public class ObjectBasicFeatures
{
    public string member;
    public Level2 level2Member; // This is to demonstrate that it will be updated in both the objects
    public ObjectBasicFeatures(string memberVal)
    {
        member = memberVal;
        level2Member = new Level2("Level 2 Value");
    }

    /// Constructor to copy member values.
    public ObjectBasicFeatures(ObjectBasicFeatures originalObject)
    {
        member = originalObject.member;
        level2Member = originalObject.level2Member;
    }

}

/// This class does not have a constructor to copy member values.
public class Level2 
{
    public string member;
    public Level2(string memberVal)
    {
        member = memberVal;
    }
}

Output of this will look like below

enter image description here

Upvotes: 1

Michał Zych
Michał Zych

Reputation: 149

You need a copy constructor or make ObjectBasicFeatures a struct (struct is value type whereas class is a reference type) Then you could write:

ALLOBJECTS[1]= new ObjectBasicFeatures(ALLOBJECTS[0]);

Another Example:

class Program
{
    static void Main(string[] args)
    {
        var o1 = new ObjectBasicFeatures();
        var o2 = new ObjectBasicFeatures(o1);
        System.Diagnostics.Debug.Assert(!o1.Equals(o2));

    }
}

public class ObjectBasicFeatures
{
    public ObjectBasicFeatures()
    {
        MyProperty = 0;
    }

    /// <summary>
    /// copy constructor
    /// </summary>
    /// <param name="other"></param>
    public ObjectBasicFeatures(ObjectBasicFeatures other)
    {
        MyProperty = other.MyProperty;
    }

    public int MyProperty { get; set; }
}

Upvotes: 3

Related Questions