JRace
JRace

Reputation: 43

How to make two classes properties reference to the same value

I am trying to do the following:

I want to have two classes Class01 and Class02. A property of Class02 Integer is initialized with my property Integer of Class01. When I change my Class01. Integer now I want to have my Class02. Integer changed as well. How can I do that?

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

Upvotes: 4

Views: 2259

Answers (5)

Serge
Serge

Reputation: 4036

If you want to share a variable between all instances of the classes, then you can create a parent class, from which both classes will inherit, and define a protected static variable in it (protected = accessible only to this class and classes that inherit from it; static = shared across the 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);
    }
}

Upvotes: 0

Aftab Ansari
Aftab Ansari

Reputation: 39

@JRace

Assigning value of Class01 Object will only Copy the value not memory reference. So whenever you make a change in one.Integer it will never ever reflect to two.Integer. You can create a new class(Reference type) with int properties, create instance of this class and work. It will help out.

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
        }
    }

Upvotes: 1

Alex
Alex

Reputation: 987

You need to distinguish between Reference Types and Value Types.

When you change a value for a Reference Type, it is changed everywhere where this object is referenced.

int is a Value Type, thus when changing integer property in one object, it would not update it automatically in the other object.

Class01 is a Reference Type, if you used it in a similar fashion to Integer property it would work the way you want it to work.

Examples of Value Types: int, double, bool, char, DateTime.

Examples of Reference Types: Form, string, List

The dirtiest solution is to have a class with a static variable, e.g. Class03 with static int which both Class01 and Class02 would be able to access and update instead of their own properties, but I do not recommend this. Depending on the architecture you may discover that you may want to write a wrapper for this value, making it a Reference Type, or use Events to notify other object about the change or use some sort of inheritance if the classes are very connected.

Upvotes: 3

Eugen M
Eugen M

Reputation: 51

I know of 1 way, it might not be the easiest to do but its sure works:

You can make a custom event handler, subscribe those classes in the event handler when they innitialize, once they are subscribed in the event handler make a public void for those classes, call that public void something like public void UpdateIntegerValue(int), inside that event handler raise that event with 1 of the parameters beying the integer value and another beying the class object itself then simply set the value for each of the class instance.

In the end, it will be as easy as to update all of the two classes with 1 line such as UpdateIntegerValueForAllClasses(Object ownerclass, int integerToUpdate);

Let me know if you didnt understand it fully or need more details.

Upvotes: 0

Roman
Roman

Reputation: 12171

You can't do it, because int is value type and every class has its' own copy of int value;

You can write class wrapper which will contain int value and set references to it in your classes or you can try box int.

Upvotes: 2

Related Questions