Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

A field initializer cannot reference the non-static field method or property

I have been programming for a few years, and I know, that the following should work perfectly fine. Just trying to initialize all variables with one common type and same value.

int count = 0, 
    xmin = count, 
    xmax = count,  
    ymin = count, 
    ymax = count;

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The correct syntax should be:

int count, xmin, xmax, ymin, ymax = 0;

EDIT:

enter image description here

Your original code seems to work no?

EDIT2:

Well ofcourse that won’t work in a field initializer!

A field initializer cannot refer to other instance fields.

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/fields

You can probs move the init code to your constructor if you really want – something like:

int count = 3, xmin, xmax, ymin, ymax;

void MyCtor()
{
    xmin = xmax = ymin = ymax = count;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading