Marek
Marek

Reputation: 111

error CS0236: A field initializer cannot reference the non-static field, method, or property

I have a problem with error CS0236.

public class Converter
{
    public string Celsiusz { get; set; }
    public string Fahrenheit { get; set; }
    public string Kelvin{ get; set; }
    public string Rankinen { get; set; }
    public string Reaumur { get; set; }
    public string Romer { get; set; }
    public string Delisle { get; set; }
    public string Newton { get; set; }


    double CelsiuszDouble;

    bool bupa = double.TryParse(Newton, out CelsiuszDouble); 
 }

Are coming two errors:

First:

error CS0236: A field initializer cannot reference the non-static field, method, or property Converter.Newton

Second:

error CS0236: A field initializer cannot reference the non-static field, method, or property Converter.CelsiuszDouble

Upvotes: 2

Views: 5885

Answers (2)

katmanco
katmanco

Reputation: 1212

I agree with @David You cant write such a code in class definition . You must write that part bool=.... inside either Constructor or a Method.

Upvotes: 3

Kinexus
Kinexus

Reputation: 12904

This code is trying to access the public property of a non-static class, therefore you have no instance of it to access.

 bool bupa = double.TryParse(Newton, out CelsiuszDouble); 

If you change the property to have a private backing field, you could use that in your code.

Upvotes: 1

Related Questions