开发者

How can I access members of a private abstract to class for read-only purposes in a public class?

I have a private abstract class called TDSeq in which there are some abstract members and non-abstract members. There are 2 derived classes which it gets data from:- private class TDSeqBuy: TDSeq and private class TDSeqSell: TDSeq.

The members from the private abstract class that I am trying to access are private/public bools/doubles/integers.

The data flows from the derived classes through to the private abstract class by protected abstract name {get;}. After which the data is "moved" to the above mentioned private/public bool/doubles/integers.

I would like to access data for read-only purposes from the abstract class to a public class but do not know how to do that. Could someone please help?

private abstract class TDSeq
{
    public event SetupCompletedEventHandler SetupCompleted;

    protected abstract double TDSTHigh { get; }
    protected abstract double TDSTLow { get; }
    protected abstract double SetupStopLevel { get; }
    public double highesthigh = 0;
    public double lowestlow = 0;
    public double truerange = 0;
    public double setupstople开发者_开发问答vel = 0;

    // ...

    case TDSTStateSetup.Completed:
        if( ValidSetup )
        {
            Print = "ValidExtSetup";
            setupCount++;
            SetupDrawText();
            //Print = NameIndex;
        }
        else
        {
            Print = "ExtSetup Finalised";
            tdsetupiscompleted = true;
            if (tdsetupiscompleted)
            {
                Print = "tdsetupiscompleted";
            }
            if (tdsetupdirection == 1) 
            {
                Print = "tdsellsetupiscompleted";
            }
            if (tdsetupdirection == -1) 
            {
                Print = "tdbuysetupiscompleted";
            }
            highesthigh = TDSTHigh;
            lowestlow = TDSTLow;
            truerange = (highesthigh - lowestlow);
            setupstoplevel = SetupStopLevel;
            stateSetup = TDSTStateSetup.Finished;
        }
// ...
}

I'm trying to publicly access the last 5 lines...


You can also use auto properties to acheive the same without using a private field.

e.g.

private abstract class A
{
    protected int Number { get; private set; }
}

private class B : A
{
    public int GetNumber()
    {
        return Number;
    }
}


Use protected, not private. Also consider composition over inheritance.

Nested classes are not a good idea. It only limits scope. And protected will not save you there.


If you want access to the properties and them only to be read only, store the values in private fields - and give a protected get property to give read only access to the private fields like so:

    private abstract class A
    {
        private int _number = 5;

        protected int Number { get { return _number; } }
    }

    private class B : A
    {
        public int GetNumber()
        {
            return Number;
        }
    }

    private class C : A
    {
        public int GetNumber()
        {
            return Number;
        }
    }


If you want to access data via an object of an abstract class A within a method of a separate, public class X, the abstract class has to be visible to X, so it has to be public (or at least internal, when A and X are part of the same assembly):

public class Program
{
    static void Main(string[] args)
    {

        B b = new B();
        X.Test(b);
    }

    // private does not work here if you want to have a parameter of type A in X
    public abstract class A
    {
        private int _number = 5;
        public int Number { get { return _number; } }
    }

    private class B : A
    {
    }
}

public class X
{
    public static void Test(Program.A a)
    {
        Console.WriteLine(a.Number);
    }
}


Top level classes in an assembly can only be public or internal in terms of accessibility, so I'm assuming your private abstract class and it's derived classes are all nested inside some public class, for starters. Correct?

If so, simply access members of the nested private abstract class that are non-abstract and public by first instantiating the private derived classes inside that parent class via say a public property, then simply call the public field from it:

public class TopClass
{

  DerivedClass MyDerivedClass;

  public int GetDerivedClassPublicField
  {
    get
      {
        DerivedClass MyDerivedClass = new DerivedClass();
        return DerivedClass.myfield;//here is access to your abstract class field from outside
      }
   }

  // Private classes must be nested
  private abstract class AbstractClass
  {
    public int myfield = 1;
  }

  private class DerivedClass : AbstractClass
  {
    ... (derived classes inherit the non-abstract field from the abstract parent by default here) ...
  }
}


// now call the public top level class property to get the field in the abstract class
TopClass MyTopClass = new TopClass();
int myInt = MyTopClass.GetDerivedClassPublicField;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜