locked
passing a property to a constructor RRS feed

  • Question

  • User1094877758 posted

    In the ModifyData class, I'd like to pass GetGridData property to the FilteredFieldList constructor.  However, the error I'm getting is:

    A field initializer cannot reference the non-static field, method, or property 'WebDataGrid.Class.ModifyData.bg'.

    If I put the 3 lines of code in a method it works fine.  I'm not sure why passing a property cannot be done on a global level of a class.

    Below is the code: 

     public class ModifyData
        {
            BindGrid bg = new BindGrid();
    
            FilteredFieldList pka = new FilteredFieldList(bg.GetGridData, true);
            FilteredFieldList ca = new FilteredFieldList(bg.GetGridData, false);

    Thanks for the help!

    Thursday, June 5, 2014 12:13 PM

Answers

  • User-760709272 posted

    Variable initialisers such as your pka and ca fields can't reference the thing that is being created.  It's just one of the "rules" of c# (.net in general?).  It's probably to avoid issues around referencing the class when it hasn't been fully created yet.  Whatever the reason, you're not allowed to do it.  You'll need to put this code in the constructor instead;

     public class ModifyData
        {
            BindGrid bg = new BindGrid();
    
            FilteredFieldList pka = null;
            FilteredFieldList ca = null;
    
            public ModifyData()
            {
                pka = new FilteredFieldList(bg.GetGridData, true);
                ca = new FilteredFieldList(bg.GetGridData, false);
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 5, 2014 6:49 PM

All replies

  • User-760709272 posted

    Variable initialisers such as your pka and ca fields can't reference the thing that is being created.  It's just one of the "rules" of c# (.net in general?).  It's probably to avoid issues around referencing the class when it hasn't been fully created yet.  Whatever the reason, you're not allowed to do it.  You'll need to put this code in the constructor instead;

     public class ModifyData
        {
            BindGrid bg = new BindGrid();
    
            FilteredFieldList pka = null;
            FilteredFieldList ca = null;
    
            public ModifyData()
            {
                pka = new FilteredFieldList(bg.GetGridData, true);
                ca = new FilteredFieldList(bg.GetGridData, false);
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 5, 2014 6:49 PM
  • User1094877758 posted

    Thanks for your response.  On a side note, is that constructor injection because you newed up the classes in the constructor? Thanks.

    Monday, June 9, 2014 10:10 PM
  • User-760709272 posted

    Not really, the constructor is needed as that is the first place you can reference the variables you have defined on the class.  The initialiser variables run first, but in that phase of class creation one variable can't access another.  After that phase comes the constrcutor and it is at this point that the initialiser variables can now be accessed and assigned to.  It's just moving the code around a little.

    Tuesday, June 10, 2014 4:38 AM
  • User1094877758 posted
    Thank you.
    Tuesday, June 10, 2014 7:34 AM