Bastyon
Bastyon

Reputation: 1611

How can I set c# object property value when its "get" is called

I'm just trying to see if there is a way I can do this.. I have two classes. One exists as a property in the other. So in a way its like a parent child class relationship.. The value of the child object however I want to set at this instant its "get" is called based on a value that exists in the parent object..only if it exists and isn't null.

I also don't want to set the child object if its already been set...I'm just gonna keep the mechanic as simple as possible this is nothing more than figuring out hot to do it.

My child Class is

class ChildObject
    {
        public int? some_val { get; set; }

    }

My parent Class is

class ParentObject
    {
        public int? some_val { get; set; }
        public ChildObject child
        {
            get {
               //if the chld hasnt been set already
                if (child == null)
                {
                    //if the value of the integer has been set
                    if(some_val != null)
                    {
                        //some some method that sets the objects value
                        //based on teh value of the integer property
                        SomeMethod();
                    }
                }
                return child;
            }
            set { child = value;}
        }
        private void SomeMethod()
        {
            int new_val = this.some_val.Value + 5;
            this.child = new ChildObject {  some_val = new_val };
        }
    }
}

in some other routine..like the program of a console app or whatever I do the following

  ParentObject p = new ParentObject ();
            p.some_val = 1;
            ChildObject c = p.child;
            int i = c.some_val.Value;

Ideally I would like to add in some checks to see if the "some_val" in the parent object is changed and if so re "set" the child object...but for now Im just trying to figure out how to set the property the first time its get is called.

For some reason when I run that it just crashes.and not with any exception. Ive tried wrapping teh routine in a try catch to see what the problem is but its just simply stops running and closes out of executing the console program.

Upvotes: 0

Views: 770

Answers (3)

Osman Villi
Osman Villi

Reputation: 346

You should be carefull about same field or value name. You should try another value and field name. Like this;

private ChildObject child;
public ChildObject Child
{
    get {
       //if the chld hasnt been set already
        if (child == null)
        {
            //if the value of the integer has been set
            if(some_val != null)
            {
                SomeMethod();
            }
        }
        return child;
    }
    set { child = value;}
}

And you should resource to encapsulation principle. Good luck.

Upvotes: 1

Vsevolod Goloviznin
Vsevolod Goloviznin

Reputation: 12324

The problem here is that when you want to add some logic to get or set methods you need to include a field that will contain the actual value. The problem arises because you actually have an infinite loop there and getting probably a SO exception. That happens because when you get your child you call it's getter which it again here: return _child;.

        public int? some_val { get; set; }
        private ChildObject _child;
        public ChildObject child
        {
            get {
               //if the chld hasnt been set already
                if (_child == null)
                {
                    //if the value of the integer has been set
                    if(some_val != null)
                    {
                        //some some method that sets the objects value
                        //based on teh value of the integer property
                        SomeMethod();
                    }
                }
                return _child;
            }
            set { _child = value;}
        }
        private void SomeMethod()
        {
            int new_val = this.some_val.Value + 5;
            this.child = new ChildObject {  some_val = new_val };
        }

Upvotes: 1

Patrick
Patrick

Reputation: 736

You can use another field to do such like so

    public int? some_val { get; set; }
    public ChildObject _child;
    public ChildObject child
    {
        get {
           //if the chld hasnt been set already
            if (_child == null)
            {
                //if the value of the integer has been set
                if(some_val != null)
                {
                    SomeMethod();
                }
            }
            return _child;
        }
        set { _child = value;}
    }



    private void SomeMethod()
    {
        int new_val = this.some_val.Value + 5;
        this.child = new ChildObject {  some_val = new_val };
    }

Upvotes: 0

Related Questions