I am getting the following compiler errors with the below code snippet:
An object reference is required for the non-static field, method, or
property
in line 5, and
A field initializer cannot reference the non-static field, method, or
property
in line 1 here at checker in ThreadStart:
public Thread tC = new Thread(new ThreadStart(checker));
public static void checker()
{
if (CheckServerState()) LabelWrite(true, Label1);
else LabelWrite(false,Label1);
}
Could anyone please explain why I get those errors?
In your first code snippet I presume that
Label1is the name of a class, not the name of a variable. You need to instantiate an object of that class. Then you can pass that object to yourLabelWrite()methodThe second compiler error means that you can’t reference to the method
checker()when you are assigning the new Thread object totCin a field initializer.You need to do that in a constructor: