7
public class TestClass
{
    public int TestNumber;
    public string TestName;

    public TestClass(string name, int number)
    {
        TestName   = name;
        TestNumber = number;
    }
}

public class AnotherClass
{  
    TestClass Var1, Var2;

    void Start()
    {
         Var1 = new TestClass("Me", 1);
         Var2 = Var1;
         Var2.TestName = "aaa";
    }
}

When I debug the value of Var1.TestName I get "aaa" but originally it was "Me". How can I separate each var but still Var2 gets its initial values from Var1?

CaptJak
  • 3,592
  • 1
  • 29
  • 50
Forenkazan
  • 97
  • 2
  • 2
  • 8
  • Not sure if it's an exact duplicate, but you might find some good information here: http://stackoverflow.com/questions/18229463/reference-type-in-c-sharp In fact, a Google search for "Stack Overflow c# reference type" finds several questions which relate to this concept, and you'll probably learn from all of them. – David Feb 14 '16 at 18:40
  • 3
    [What is the difference between a variable, object and reference?](http://stackoverflow.com/questions/32010172/what-is-the-difference-between-a-variable-object-and-reference/32010236#32010236) and also [C# Concepts Value vs Reference Types](http://www.albahari.com/valuevsreftypes.aspx) – Steve Feb 14 '16 at 18:43
  • You shouldn't expose your fields to user. They should be `private` or `protected`, but almost never public. – Simply Me Feb 14 '16 at 19:07

2 Answers2

5

Here is your problem:

Var1=new TestClass("Me",1);
Var2 = Var1;
Var2.TestName="aaa";

Var2 = Var1; is actually a reference copy! This means that the Var2 will take the address of Var1 and no matter what you modify in either of them, it will be visible in the other. To get that, I would recommend using a copy of Var2. To do so, create a method in your testClass class.

public testClass copy()
{
    testClass tC = new testClass(this.TestNumber, this.TestName);
    return tC;
}

Now you can assign the value like this: Var2 = Var1.copy();

Simply Me
  • 1,579
  • 11
  • 23
0

C# is a pass by reference language, when dealing with objects. So when you say Var2 = Var, you're saying that Var2 now holds the address of whatever address Var1 was holding, effectively making them point to the same object.

One work around is to turn it into a pass by value, like so:

public void Start()
{
    Var1 = new TestClass("Me", 1);
    Var2 = new TestClass();           // You would need a default constructor, and to use it to prevent the null exception error

    Var2.TestNumber = Var1.TestNumber;
    Var2.TestName   = "aaa";

}

Or, if you will be using more values and this is an overly simplified example, you can use another approach:

public void Start()
{
    Var1 = new TestClass("Me", 1);
    Var2 = Var1.GetValues();
}

And in your test class:

public testClass GetValues() => return new testClass(TestNumber, TestName);
Community
  • 1
  • 1
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
  • Thanks, that helped :) but what is that "=>" ? – Forenkazan Feb 14 '16 at 18:56
  • 1
    No problem :) And that is a little shorthand for writing methods in one line of code, I think its very clean :) They're called expression bodied functions. You can find out more about them, and all the other C# 6 features, here: https://msdn.microsoft.com/en-us/magazine/dn802602.aspx :) – AustinWBryan Feb 14 '16 at 19:02