AlreadyLost
AlreadyLost

Reputation: 827

override values of a property from base class in C#

I have class A that inherits from class B, class B has a property like this

public configObject config
{
    get
    {
        var config = new configObject
        {
            property1 = value1
            property2 = value2;
            property3 = null
            property4 = false;
        };

        return config;
    }

    set { this.config = value; }
}

In class A, I am trying to override values of some fields of this.config but for some reason the values are not getting updates with the assignments in A

Public class A: B
{
    public A()
    {
    }

    public configJson
    {
         get
            {
                this.config.property2 = newValue2;
                this.config.property3 = newValue;
                this.config.property4 = true;
                return this.SerializeConfiguration(this.config);
            }
    }
}

Any idea where I am making a mistake? Appreciate any help!

Upvotes: 1

Views: 1713

Answers (2)

vbnet3d
vbnet3d

Reputation: 1151

TLDR: The best option is rewriting the get method of class B to reuse the same object rather than recreating it every time.

This is being caused by the get method from your parent class. You can get around this in the child class if you do it as follows:

public configJson
{
     get
        {
            configObject cfg = new configObject();
            cfg.property1 = this.config.property1;
            cfg.property2 = newValue2;
            cfg.property3 = newValue;
            cfg.property4 = true;
            return this.SerializeConfiguration(cfg);
        }
}

Here's why it's a problem:

Every time that you use this.config, it calls:

get
{
    var config = new configObject
    {
        property1 = value1
        property2 = value2;
        property3 = null
        property4 = false;
    };

    return config;
}

Which means that the object is recreated every time.

I would recommend rewriting your parent class so you don't instantiate the values in the get method.

A quick way to do that is this:

class B {
    private configObject cfg;
    public configObject config {
        get {
             if (cfg == null)
                 cfg = new configObject
                 {
                     property1 = value1
                     property2 = value2;
                     property3 = null
                     property4 = false;
                 };

                 return cfg;
             }
         set { cfg = value; }
}

Upvotes: 1

Ron Beyer
Ron Beyer

Reputation: 11273

You can override properties with the new operator. Lets say you have class B:

public class B
{
    public configObject config
    {
        get
        {
            var config = new configObject
            {
                property1 = value1
                property2 = value2;
                property3 = null
                property4 = false;
            };

            return config;
        }

        set { this.config = value; }  //This is actually a StackOverflow error!
    }
}

And you override that with A:

public class A
{
    public new configObject config
    {
        get
        {
            var baseConfig = base.config;
            //Modify baseConfig properties here
            return baseConfig;
        }
        set { base.config = value; }
    }
}

So now you've overridden the values from the base class.

Upvotes: 0

Related Questions