公开课程中的字段

时间:2018-07-19 06:32:26

标签: c# class parameters

我是C#和对象编码的新手,所以请保持柔和。...

我有一个课堂通话LED,请参见下文:

public sealed class LED
{
    public GpioPinValue ReqPinValue { get; set; }     //Enable/Disable LED
    public bool Flashing { get; set; }      //Does the LED flash
    public GpioPin Pin { get; set; }
    public int flashingPeriod { get; set; } //Period to flash in seconds



    private GpioPinValue value; //Pin value (high/low)
    private int flashCount = 0; //Times we have entered the timer loop


    public LED()
    {
    }

    public void UpdateLED()
    {
        int timesToCycle = 0;

        if (ReqPinValue == GpioPinValue.Low)
        {
            if (Flashing)
            {
                timesToCycle = flashingPeriod * 2;
                if (flashCount == timesToCycle)
                {
                    value = (value == GpioPinValue.High) ? GpioPinValue.Low : GpioPinValue.High;
                    Pin.Write(value);
                    flashCount = 0;
                }
                else
                    flashCount++;
            }
            else
            {
                Pin.Write(GpioPinValue.Low);
            }
        }
        else
        {
            Pin.Write(GpioPinValue.High);
        }
    }

}

在另一类中,我为4个不同的状态LED创建该LED类的四个实例。

public sealed class StatusLED
{
    private const int RUN_LED = 4;
    private const int IO_LED = 17;
    private const int NET_LED = 27;
    private const int FAULT_LED = 22;

    public LED RunLed = new LED();
    public LED IOLed = new LED();
    public LED NetLed = new LED();
    public LED FaultLed = new LED();

    private GPIO GPIO = new GPIO();
    private GpioController gpioController;

    private ThreadPoolTimer timer;


    public void InitStatusLED()
    {
        gpioController = GPIO.InitGPIO();

        if (gpioController == null)
        {
            Debug.WriteLine("Failed to find GPIO Controller!");
            //TODO proper error handling although this should never happen
        }
        else
        {
            //Setup the default parameters for the LEDS (ie flashing or non-flashing)
            RunLed.Flashing = false;
            IOLed.Flashing = false;
            NetLed.Flashing = false;
            FaultLed.Flashing = false;

            RunLed.flashingPeriod = 0;
            IOLed.flashingPeriod = 0;
            NetLed.flashingPeriod = 0;
            FaultLed.flashingPeriod = 0;

            RunLed.Pin = GPIO.InitOutputPin(gpioController, RUN_LED);
            IOLed.Pin = GPIO.InitOutputPin(gpioController, IO_LED);
            NetLed.Pin = GPIO.InitOutputPin(gpioController, NET_LED);
            FaultLed.Pin = GPIO.InitOutputPin(gpioController, FAULT_LED);

            //Turn the LED's on to Start
            RunLed.ReqPinValue = GpioPinValue.Low;
            IOLed.ReqPinValue = GpioPinValue.Low;
            NetLed.ReqPinValue = GpioPinValue.Low;
            FaultLed.ReqPinValue = GpioPinValue.Low;

            timer = ThreadPoolTimer.CreatePeriodicTimer(Timer_Tick, TimeSpan.FromMilliseconds(500));
        }
    }
    private void Timer_Tick(ThreadPoolTimer timer)
    {
        RunLed.UpdateLED();
        IOLed.UpdateLED();
        NetLed.UpdateLED();
        FaultLed.UpdateLED();
    }
}

我现在想使用下面的代码为另一个类中的StatusLED类中的这些实例设置字段“ ReqPinValue”

private StatusLED statusLED = new StatusLED();

statusLED.RunLed.ReqPinValue = GpioPinValue.Low;

我收到以下错误:

  

错误:类型'....'包含外部可见字段'....'   只能通过结构来暴露。

我看到它不喜欢下面的行是公开的,如何在不将其公开的情况下从另一个类访问该实例的参数?

public LED RunLed = new LED();

1 个答案:

答案 0 :(得分:0)

您可以设置以下 properties 而不是字段:

public LED RunLed {get;set;}
public LED IOLed {get;set;}
public LED NetLed {get;set;}
public LED FaultLed {get;set;}

然后,在您的InitStatusLED方法中,将它们初始化:

public void InitStatusLED()
{
    gpioController = GPIO.InitGPIO();

    if (gpioController == null)
    {
        Debug.WriteLine("Failed to find GPIO Controller!");
        //TODO proper error handling although this should never happen
    }
    else
    {
        //Setup the default parameters for the LEDS (ie flashing or non-flashing)
        RunLed = new LED(GPIO.InitOutputPin(gpioController, RUN_LED));
        IOLed = new LED(GPIO.InitOutputPin(gpioController,IO_LED));
        //And so on

但是等等-您的LED类没有接受GpioPin的构造函数。让我们也修复它:

public sealed class LED
{
    public GpioPinValue ReqPinValue { get; set; }     //Enable/Disable LED
    public bool Flashing { get; set; }      //Does the LED flash
    public GpioPin Pin { get; set; }
    public int flashingPeriod { get; set; } //Period to flash in seconds



    private GpioPinValue value; //Pin value (high/low)
    private int flashCount = 0; //Times we have entered the timer loop


    public LED(GpioPin pin)
    {
        Pin = pin;
        Flashing = false;
        flashingPeriod = 0;
        ReqPinValue = GpioPinValue.Low;
    }

并了解我们如何也从您的InitStatusLed方法中消除了整个重复代码。

完成所有这些操作后,返回并再次查看您的属性。对于只应由类本身设置 而不是由其他任何代码使用设置的类,请使用设置器private

public GpioPin Pin { get; private set; }