Daniel
Daniel

Reputation: 31559

Overriding property attributes

I have a base class with a field:

class Base
{
    public int A;
    public int ShowA()
    {
        Console.WriteLine(A);
    }
}

I have a derived class:

class Derived : Base
{
    public Derived()
    {
        A = 5;
    }
}

I use a GUI editor that shows all fields for a class. Because Derived sets A in its constructor I don't want it to show in the editor. The editor doesn't fields the have [HideInInspector()] attribute.
How can I make Derived have its A property with [HideInInspector()] attributes while Base won't? I can't use keyword new because I want the Base still to use the same field as Derived in its functions (e.g. (new Derived()).ShowA() will print 5).

Edit: It looks tricks with properties and new won't work because the GUI editor treats fields with new twice (once for base, and once for derived).

Upvotes: 2

Views: 2057

Answers (1)

Stecya
Stecya

Reputation: 23266

Try this

class Base
{
    public virtual int A {get; set;}
    public int ShowA()
    {
        Console.WriteLine(A);
    }
}

class Derived : Base
{
    public Derived()
    {
        A = 5;
    }

    [HideInInspector()]
    public override int A
    {
        get { return base.A;}
        set { base.A = value}
    }
}

Upvotes: 3

Related Questions