How to make properties of two classes a reference to the same value

I am trying to do the following:

I want to have two classes Class01and Class02. The property Class02 Integeris initialized with my property Integerof Class01. When will I change mine Class01. Now Integer I want to have Class02. The whole change has also changed. How can i do this?

Class01 one = new Class01 { Integer = 16 };
Class02 two = new Class02 { Integer = one.Integer };

Console.WriteLine("Class one: {0} -- Class two: {1}", one.Integer, two.Integer); 
// Prints: Class one: 16 -- Class two: 16

one.Integer++;

Console.WriteLine ("Class one: {0} -- Class two: {1}", one.Integer, two.Integer); 
// Prints:             Class one: 17 -- Class two: 16
// I want it to print: Class one: 17 -- Class two: 17
+4
source share
5 answers

You need to distinguish between link types and value types.

When a value is changed for a reference type, it changes wherever this object refers.

int is a value type, therefore, when changing an integer property in one object, it will not automatically update it in another object.

Class01 , Integer, , , .

: int, double, bool, char, DateTime.

: , ,

- static, . Class03 static int, Class01, Class02 , . , , , - , .

+3

, int - , int;

, int box int.

+2

@JRace

Class01 , . , one.Integer, two.Integer. ( ) int, . .

public class Class02
    {
        public Data Integer { get; set; }
    }
    public class Class01
    {
        public Data Integer { get; set; }
    }
    public class Data {

        public int Integer { get; set; }
    }
    public class A
    {
        private void myFunc()
        {
            Data data = new Data();
            data.Integer = 16;
            Class01 one = new Class01 { Integer = data };
            Class02 two = new Class02 { Integer = data };
            Console.WriteLine("Class one: {0} -- Class two: {1}", one.Integer.Integer, two.Integer.Integer);
            // Prints: Class one: 16 -- Class two: 16
            data.Integer++;
            Console.WriteLine("Class one: {0} -- Class two: {1}", one.Integer.Integer, two.Integer.Integer);
            // Prints:             Class one: 17 -- Class two: 16
            // I want it to print: Class one: 17 -- Class two: 17
        }
    }
+1

, , :

, , , , void , void public void UpdateIntegerValue (int), , 1 , - , .

, 1 , UpdateIntegerValueForAllClasses (Object ownerclass, int integerToUpdate);

, .

0

If you want to share a variable between all instances of classes, you can create a parent class from which both classes inherit and define a variable in it protected static( protected= available only for this class and classes that inherit from it; static= shared by type):

abstract class ClassAbstract{
    protected static int _myInteger {get;set;}
}

class Class01:ClassAbstract{
    public int MyInteger {
        get{return _myInteger;}
        set{ _myInteger=value;}
    }
}

class Class02:ClassAbstract{
    public int MyInteger {
        get{return _myInteger;}
        set{ _myInteger=value;}
    }
}

static class Program {
    public void Main(){
        Class01 one = new Class01();
        one.MyInteger = 16;
        Class02 two = new Class02();
        Console.WriteLine("Class two: {0}", two.MyInteger);
    }
}
0
source

All Articles