Home How Do I Make Hidden Property In Base Class Get Only
Reply: 2

How Do I Make Hidden Property In Base Class Get Only

Cheyenne
1#
Cheyenne Published in 2017-12-07 19:47:07Z

I have the following situation and I want to know how I can make the property SomeNumber to be read only from instances of class A? BTW, I am using C# latest stable build, or not 7.2 c#, so I can't use private protected modifier.

So I don't want to be able to set SomeNumber on accident from class A. I know it can be written in another way to achieve it but can I make the setter in class A's property only visible to class B's property setter? So you can still read SomeNumber from instances of A. Thanks.

I want to to this because I have multiple classes, not only B, derived from class A that hides the base property and they all need to run their own methods when setting SomeNumber.

class A
{
    private int someNumber;
    internal int SomeNumber
    {
        get { return someNumber; }
        set
        {
            someNumber = value;
            SomethingNotSpecificForDerivedClasses();//I prefer not to move this to each derived setter! There are more derived classes than just B, fyi
        }//I NEED TO MODIFY THE ACCESS OF THIS
    }

    private void SomethingNotSpecificForDerivedClasses()
    {
        ...
    }
}
class B : A
{
    new internal int SomeNumber//hides base member
    {
        get { return base.SomeNumber; }
        set
        {
            base.SomeNumber = value;//ONLY THIS SETTER SHOULD BE ALLOWED TO DO THIS
            SomethingINeedToRunWhenSettingSomeNumber();
        }
    }

    private void SomethingINeedToRunWhenSettingSomeNumber()
    {
        ...
    }
}
Daniel Grankin
2#
Daniel Grankin Reply to 2017-12-07 20:31:11Z

If you want to run a particular method in the setter perhaps you can simply declare a virtual method?

public class A
{
    private int _someNumber;

    internal int SomeNumber
    {
        get { return _someNumber; }
        set
        {
            _someNumber = value;
            SomethingINeedToRunWhenSettingSomeNumber();
        }
    }

    protected virtual void SomethingINeedToRunWhenSettingSomeNumber()
    {
        // Do something generic to A.
    }
}

public class B : A
{
    protected override void SomethingINeedToRunWhenSettingSomeNumber()
    {
        base.SomethingINeedToRunWhenSettingSomeNumber();

        // Do something specific to B.
    }
}
Jeff
3#
Jeff Reply to 2017-12-07 20:14:16Z

Just like with any other override method, you need to make your base class method virtual, and use the 'override' keyword on your subclass.

Within the class, you can then call the base class' method to set the value, so it will run the base-class setter.

Your end result could look like this:

Class A
{
    private int someNumber;
    internal virtual int SomeNumber
    {
        get { return someNumber; }
        set { someNumber = value; }
    }
}
Class B : A
{
    internal override int SomeNumber
    {
        get { return base.SomeNumber; }
        set
        {
            base.someNumber = value;
            SomethingINeedToRunWhenSettingSomeNumber();
        }
    }

    private void SomethingINeedToRunWhenSettingSomeNumber()
    {
        ...
    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.318347 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO