Search code examples
c#propertiesreferencedependencies

Assign property address to another property (reference). Change property when another property is changed. c#


I would like to assing one property to another in different class. Change this property automatically when passed property is changed. Something like reference the same address in c++. Is it possible in C#?

I want to achieve effect that when base property "IsOpen" in PressureValve class is changed, in the same time automatically propert "IsEnabled" in Component class is changed.

Thank you in advance for any help.

Classes:

    class Component
    {
        public Component(int id, string description, ref anotherPropertyFromDifferentClass)
        {
            Id = id;
            Description = description;
            //Assign IsEnabled to another property, change it always when anotherPropertyFromDifferentClass is changed.
            IsEnabled = ref anotherPropertyFromDifferentClass;
        }
        public int Id { get; set; }
        public string Description { get; set; }
        public bool IsEnabled { get; set; }
    }
    class PressureValve
    {
        public bool IsOpen { get; set; }
    }

Program:

            PressureValve pressureValve = new PressureValve();
           
            //status read from external device, changing during programm running
            pressureValve.IsOpen = externalInfo;
            //create component, change component IsEnabled property automatically when pressureValve.IsOpen is changed
            Component component = new Component(1,"Valve status", ref pressureValve.IsOpen)



 

Edit: I would like to extend functionality suggested in accepted answer. How to assign property from CommonProperty dynamically?

Example, more than one property in CommonPropertyclass:

    class CommonProperty //Change this class name to Your desired one.
{
    public bool IsOpen { get; set; }
public bool IsPoweredUp{ get; set; }
public bool IsRunning{ get; set; }

}

Then I would like to have multiple objects of Component class

class Component
{
    public int Id { get; set; }
    public string Description { get; set; }
    public bool IsEnabled =>  //Here to assign property dynamically while creating object.
    public CommonProperty ValveStatus { get; } //Set this only on Constructor since we want to update it automatically through the object reference

    public Component(int id, string description, CommonProperty valveStatus)
    {
        Id = id;
        Description = description;
        ValveStatus = valveStatus;
        //Assign IsEnabled to another property, change it always when anotherPropertyFromDifferentClass is changed.
    }
}

And creation of object:

//create component, change component IsEnabled property automatically when assigned property from pressureValve is changed
Component component = new Component(1, "Valve status", commonProp, propertyName or type?);

Solution

  • You can use ref int to accomplish it, but would need more wiring.

    This is the easiest way to do it and much cleaner to maintain. Also in the future if there are multiple parameters to auto-change, you can simply add them to the CommonProperty class:

    Declare a Class for the properties to auto update:

    class CommonProperty //Change this class name to Your desired one.
    {
        public bool IsOpen { get; set; }
    }
    

    Update the existing class definitions:

    class PressureValve
    {
        public CommonProperty ValveStatus { get; set; }
    }
    
    class Component
    {
        public int Id { get; set; }
        public string Description { get; set; }
        public bool IsEnabled => ValveStatus.IsOpen; //Only Getter
        public CommonProperty ValveStatus { get; } //Set this only on Constructor since we want to update it automatically through the object reference
    
        public Component(int id, string description, CommonProperty valveStatus)
        {
            Id = id;
            Description = description;
            ValveStatus = valveStatus;
            //Assign IsEnabled to another property, change it always when anotherPropertyFromDifferentClass is changed.
        }
    }
    

    This is how you can you can accomplish it easiest without much change,

    Here's how to test it:

    PressureValve pressureValve = new PressureValve();
    
    var commonProp = new CommonProperty {IsOpen = externalInfo};
    
    //status read from external device, changing during programm running
    pressureValve.ValveStatus = commonProp;
    //create component, change component IsEnabled property automatically when pressureValve.IsOpen is changed
    Component component = new Component(1, "Valve status", commonProp);
    
    //Before Change
    Console.WriteLine($"pressureValve IsEnabled: {pressureValve.ValveStatus.IsOpen.ToString()}");
    Console.WriteLine($"component IsOpen: {component.IsEnabled.ToString()}");
    
    //Change commonProp
    commonProp.IsOpen = false; //since its an object, all the references in pressureValve & component will update
    Console.WriteLine($"IsOpen Set to False");
    
    //Check After Value Change
    Console.WriteLine($"pressureValve IsEnabled: {pressureValve.ValveStatus.IsOpen.ToString()}");
    Console.WriteLine($"component IsOpen: {component.IsEnabled.ToString()}");