4

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?

1
  • 1
    It's not because it's a gui component. The error message gives you the reason. I'm not sure why that's not allowed, probably because of the order in which the class' various parts are initialized. Commented Jul 15, 2010 at 10:00

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.