开发者

Field initializer cannot reference non-static field

I have a class:

public class LED
{
    public System.Windows.Forms.Label lbl;

    public LED(System.Windows.Forms.Label lblLED)
    开发者_开发知识库{
        lbl = lblLED; 
    }

    public void blink(System.Drawing.Color color, int pattern)
    {
        // ...
    }
}

and I'm creating an instance of it in a top-class:

    public LED LED1 = new LED(lblLED1); // (1)

    public void update_LED(Label lbl, double i)
    {
        //LED LED1 = new LED(lblLED1); // (2)
    }

in case (2) it allows me to pass lblLED1 inside the constructor, but in case (1) it says:

A field initializer cannot reference the non-static field, method, or property 'lblLED1'

What's the problem?


http://msdn.microsoft.com/en-us/library/5724t6za%28VS.80%29.aspx

you cant use references to fields to initialize fields in the same class outside of a method, maybe because the order in which the reference variables are initialized is not guaranteed.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜