Raunts
Raunts

Reputation: 76

c# Set Parent class property in child setter

Im trying to set a parent class property in child property setter. I have one main class :User, which has a child class LIST ArrayPositions, which in turn has a child class list of ExpressionMember. Whenever the property ExpressionMemValue in ExpressionMember class is set, i want to update it's parent class ArrayPosition aswell. However the current solution does not update the corresponding parent. Here's the code:

   public List<User> users = new List<User>();
    public class User 
    {
        public string ImageName { get; set; }
        private string _PartName = "";
        public string PartName
        {
            get
            {
                return this._PartName; 
            }
            set { 
                _PartName=value; 
            }
        }
        public List <ArrayPosition> ArrayPositions { get; set; }
        public override string ToString()
        {
            return this.PartName.ToString();
        }
    }
    public class ArrayPosition:User 
    {
        public string myArrayPos = "";
        public string PartId { get; set; }
        public string ArrayPos
        {
            get
            {              
                return this.myArrayPos;                 
            }
            set
            {
                this.myArrayPos = value;
            }

        }
        public List<ExpressionMember> ExpressionMembers { get; set; }
    }        
    public class ExpressionMember : ArrayPosition
    {
        public string ExpressionMem { get; set; }
        public string MyExpressionMemValye="";            
        public string ExpressionMemValue
        {
            get
            {
                return MyExpressionMemValye;                
            }
            set
            {                   
                MyExpressionMemValye = value;
               // this.ArrayPos = value;  //set parent value, this will not update it                           
            }

        }          
    }

Upvotes: 0

Views: 5406

Answers (2)

smelch
smelch

Reputation: 2543

It would appear that you need to not use inheritance and instead use composition which you are kind of already doing. Try doing this instead. It's not perfect by any means but I'm trying not to change your general strategy too much.

public class User 
{
    public string ImageName { get; set; }
    private string _PartName = "";
    public string PartName
    {
        get
        {
            return this._PartName; 
        }
        set { 
            _PartName=value; 
        }
    }
    public List <ArrayPosition> ArrayPositions { get; set; }
    public override string ToString()
    {
        return this.PartName.ToString();
    }
}
public class ArrayPosition 
{
    public string myArrayPos = "";
    public string PartId { get; set; }
    public string ArrayPos
    {
        get
        {              
            return this.myArrayPos;                 
        }
        set
        {
            this.myArrayPos = value;
        }

    }

    public List<ExpressionMember> ExpressionMembers { get; set; }
}        
public class ExpressionMember
{
    private ArrayPosition _parentArrayPosition;
    public string ExpressionMem { get; set; }
    public string MyExpressionMemValye="";            
    public string ExpressionMemValue
    {
        get
        {
            return MyExpressionMemValye;                
        }
        set
        {                   
            MyExpressionMemValye = value;
            this._parentArrayPosition.ArrayPos = value;
        }

        public ExpressionMember(ArrayPosition parent) {
            _parentArrayPosition = parent;
        }
    }          
}

You are definitely not using inheritance and composition correctly. You are looking to build a tree of objects where the object itself has child objects. Something that might clarify things in your mind is instead of calling them child/parent classes, refer to them as sub/super classes in the case of inheritance and parent/child objects in the case of composition. A parent object is an instance of a class that contains another instance of a class (child object). A subclass inherits the members of another class.

Upvotes: 1

F. Fix
F. Fix

Reputation: 131

Your inheritance is very strange. The exact responsibilities of your classes are not clear to me. Apart from that, you could protect the property ExpressionMembers by making it read-only. Implement a new method to add or remove elements. Add an event (e.g. ExpressionMemValueChanged) to ExpressionMember . This event is triggered when an item is added. Whenever an element is added or removed you register/deregister ArrayPosition to/from this event. Inside the event handler you can then set your ArrayPos value.

(You can use an ObservableCollection for your ExpressionMembers and react to the CollectionChanged event instead of writing a getter/setter.)

Upvotes: 0

Related Questions