Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi, a little question about access properties

Status
Not open for further replies.

perencia

Programmer
Dec 30, 2004
1
ES
Can someone explain me what's wrong with this code ?

public class a {
private string patr = @"\w@\w\.\w";
private Regex regexp = new Regex(patr);
....

I know if i change the accesor modifier of patr from private to static it runs, but i'd like to know why ..

Thanks!.
 
As you can see the compiler shows:
"A field initializer cannot reference the nonstatic field, method, or property ".
The solution is, for example , to initialize the field in the class constructor:
Code:
public class a {
private string patr = @"\w@\w\.\w";
private Regex regexp;
public a()
{
 regexp = new Regex(patr);
}
....
}
The string patr = @"\w@\w\.\w";
initialization is working because the right member is a built-in type.
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top