locked
c# a field initializer cannot reference the non-static field method or property + unit of work RRS feed

  • Question

  • User-875744750 posted

    when i try to share one instance of unit of work i get this error compiler in mvc controller code

    c# a field initializer cannot reference the non-static field method or property

    how o fix this?

    this my Business logic

    BLLBase

    public abstract class BLLBase
    {
            private UniteOfWork _unitOfWork  ;
            
            public UniteOfWork UniteOfWork 
            {
                get 
                {
                    return _unitOfWork ;
                }
    
                set {
                    _unitOfWork = value;
                }
            }
    }

    the BLL classes

    public class ExpenseBLL :BLLBase
    { // the rest of code public ExpenseBll(UnitOfWork unitofwork){ this.UniteOfWork = unitOfWork;
    } }
    public class InvoiceBLL :BLLBase
    { // the rest of code
    public InvoiceBLL (UnitOfWork unitofwork)
    {

    this.UniteOfWork = unitOfWork;
    }
    }

    a controller codes

    UnitOfWork unitofWork = new UnitOfWork();
    
    InvoiceBll invoiceBll = new InvoiceBll(unitofWork);
    ExpenseBll expenseBll = new ExpenseBll(unitofWork);

    Thursday, January 10, 2019 2:26 PM

All replies

  • User475983607 posted

    The error means the code is trying to initialize an instance type with another instance type where there is not way to guarantee the order.

    The sample code shown is full of syntax errors and does not compile.  Obviously the code shown is not the same as the code that is causing the compiler error.

    Thursday, January 10, 2019 7:40 PM
  • User1520731567 posted

    Hi bensam16,

    Where is the error?

    Could you please post more details?

    This feature generally prevents you from initialization of the field with something which might not be yet ready.

    I think you could refer to these links which similar to your issue:

    https://stackoverflow.com/questions/15204420/a-field-initializer-cannot-reference-the-non-static-field-method-or-property

    https://www.codeproject.com/Questions/793644/Error-A-field-initializer-cannot-reference-the-non

    Best Regards.

    Yuki Tao

    Friday, January 11, 2019 9:53 AM
  • User-875744750 posted

    mgebhard

    The sample code shown is full of syntax errors and does not compile.  Obviously the code shown is not the same as the code that is causing the compiler error.

    how?

    Yuki Tao

    Where is the error?

    Could you please post more details?

     i try to share one instance of unit of work to avoid  An entity object cannot be referenced by multiple instances of IEntityChangeTrackerexeption

    when i have instantiate a unit of work at controller in mvc application, i get this error

    c# a field initializer cannot reference the non-static field method or property

    controller code:

    UnitOfWork unitofWork = new UnitOfWork();
    
    InvoiceBLL invoiceBll = new InvoiceBLL(unitofWork);
    ExpenseBLL expenseBll = new ExpenseBLL(unitofWork);

    how to fix this?

    Friday, January 11, 2019 9:06 PM
  • User475983607 posted

    how?

    You posted code that does not compile and I'm unable to reproduce the issue after fixing all the syntax errors.  

    I explained the error and the error is easily Googled.   Also, the compiler or error message show the line of code(s) that cause the issue.  Start there...

    Friday, January 11, 2019 9:30 PM