Reputation: 14245
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?
Upvotes: 4
Views: 1732
Reputation: 11808
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.
Upvotes: 3