In c#,I am using the following code:
IPAddress localaddress=IPAddress.Parse('127.0.0.1');
to get a System.Net.IPAddress instance which am using in:
IPEndPoint ip= new IPEndPoint(localaddress,5555);
I am however getting an error saying:
A field initializer cannot reference the nonstatic field, method, or property 'WindowsApplication1.lanmessenger.localaddress' .
Please help.
I’m guessing your code looks like this:
The problem here is that the compiler doesn’t want you using initialized fields the way you are. You’re using
localaddressto initializeip, which is problematic from the compiler’s prospective. Two ways to get around this:Inline it:
Or just do it in the constructor: (generally better)