none
A field initializer cannot reference the nonstatic property RRS feed

  • Question

  • I get this error when trying to reference a property in my class and I don't know how to fix it.

        public class Foo
        {
            //Create fields
            private float _wmSize;
            
    
            //Create Properties that reference fields
            public float wmSize { get { return _wmSize; } set { _wmSize = value; } }
    
            //Create a constructor        
            public DcPdfMerge(float wmsize = 60)
            {
                wmSize = wmsize;            
            }
    
            //Here on wmSize is where I get the error: a field initializer cannot reference the nonstatic property Foo.wmSize
            float test = wmSize;
    
        }

    Wednesday, June 27, 2018 10:52 AM

All replies

  • Its exactly what the errors says. You cannot initialise a field from another field or method/property because there is no guarantee what order fields may get initialized and at initialization time the class is not full constructed.

    You would have to set test inside your constructor.

    public class Foo
        {
            //Create fields
            private float _wmSize;
            
    
            //Create Properties that reference fields
            public float wmSize { get { return _wmSize; } set { _wmSize = value; } }
    
            //Create a constructor        
            public DcPdfMerge(float wmsize = 60)
            {
                wmSize = wmsize; 
                test = wmSize;
            }
    
            float test;
        }

    Wednesday, June 27, 2018 10:58 AM
  • I understand but how would I be ale to use the value of 'test' in a method.
    Wednesday, June 27, 2018 1:03 PM
  • You can use any field in any method of the same class. That isn't what the error is. The error is that you're trying to initialize the test field with the value of another field. This isn't allowed in the field initializer syntax because the order in which fields are initialized is undefined. At the point that test is being initialized, wmSize may or may not be set yet.

    To initialize a field based upon the value of another (non-const) field you'll need to do it in the constructor. 

    public class Foo
    {
       public Foo ( float wmsize = 60 )
       {  
          //Field initializers have already run so do any work
          //here that relies on them being set
          _wmSize = wmsize;
    
          test = _wmSize;
       }
    
       //This isn't a constructor, constructors must follow the type name, this won't even compile
       //public DcPdfMerge ( float wmsize = 60 ) { }
    
       public float wmSize
       {
          get { return _wmSize; }
          set { _wmSize = value; }
       }
    
       private float _wmSize:
       private float test;
    }

    Do note that wmSize and test are 2 separate fields. After the ctor (constructor) runs test will no longer be synced with wmSize. If you want to sync them up then consider making test a calculated property instead.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, June 27, 2018 2:06 PM
    Moderator