-2

I'm a bit stuck on my coding.
So I'm making a progressbar, but I'm experimenting with OOP programming in C#. I have two classes, a CProgressBar and ProgressBar class.
ProgressBar inherits from CProgressBar and gets its variables but I want to make two of the variables in CProgressBar public, but still keep them private in ProgressBar.
I should also mention that this is for Unity3D, hence the RawImg class.

UPDATE: So after some people commented some ways to do this, I feel the answers do not solve my particular issue. I shall re-phrase.

  • I would like CProgressBar and ProgressBar to be visible in the Unity editor. One of the answers shows CProgressBar class being abstract, but this hides the class in the Unity editor, so that is out.
  • Both mat and rawImg should be hidden on CProgressBar, but shown on ProgressBar.
  • The variables don't necessarilly need less access, they just need to be hidden on the base class.
  • Usage of the [HideInInspector] attribute would be a hack but not sure where to use it.

    // public CProgressBar.cs
    public abstract RawImage rawImg
    {
        get;
    }
    public abstract Material mat
    {
        get;
    }
    
    // public ProgressBar.cs
    public override RawImage rawImg => null;
    public override Material mat => null;
    

    If I put [HideInInspector] attribute on the base class variables, it will also hide them on the inherited class. I don't want that.

I guess for now I will have to make both variables public allowing my users to change things that I DON'T WANT THEM TO CHANGE.

MutantSFox
  • 18
  • 6
  • "I want to make two of the variables in CProgressBar public, but still keep them private in CProgressBar." – Evan Trimboli Mar 24 '18 at 10:50
  • As described in the [documentation](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/restricting-accessor-accessibility#access-modifiers-on-overriding-accessors) you cannot change the accessibility modifier when `override`ing a property – UnholySheep Mar 24 '18 at 10:52
  • 1
    On second look you are attempting to `override` member *variables*, which is simply not allowed in C# – UnholySheep Mar 24 '18 at 10:55
  • 3
    Seems like an [xy problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Mat J Mar 24 '18 at 10:56
  • Even if it is property, `private` members cannot be marked virtual let alone override. – Mat J Mar 24 '18 at 10:58
  • 3
    He should declare the properties as `protected` and override those instead of the `private` backing fields. He has the leading-underscore convention backwards. – McGuireV10 Mar 24 '18 at 11:00
  • You can't do this. The `new` keyword as modifier is very close to what you want to do but that's it. Closing as a dup. – Programmer Mar 24 '18 at 11:28

1 Answers1

0

The code is wrong. It depends on what you want to make.

In your code you are trying to override a member; this is not possible. And why there is a private property that need to be changed.

public abstract class CProgressBar : MonoBehaviour
{
    public virtual RawImage RawImg
    {
        get;
        protected set;
    }

    public virtual Material Mat
    {
        get;
        protected set;
    }
}

public class ProgressBar : CProgressBar
{
    public ProgressBar()
    {
        Mat = null;
        RawImg = null;
    }
}

or

public abstract class CProgressBar : MonoBehaviour
{
    protected abstract RawImage RawImg
    {
        get;
    }

    protected abstract Material Mat
    {
        get;
    }
}

public class ProgressBar : CProgressBar
{
    protected override RawImage RawImg => null;

    protected override Material Mat => null;
}

or

public abstract class CProgressBar : MonoBehaviour
{
    protected virtual RawImage _rawImg
    {
        get => rawImg;
        private set => rawImg = value;
    }

    protected virtual Material _mat
    {
        get => mat;
        private set => mat = value;
    }

    private RawImage rawImg;
    private Material mat;
}

public class ProgressBar : CProgressBar
{
    protected override RawImage _rawImg => null;

    protected override Material _mat => null;
}

I know this is a stupid example. When you need a property this is simply not the correct way how to use it. Try to define your problem more specifically.

Julo
  • 1,102
  • 1
  • 11
  • 19