8

Following is my code :

private BitsManager manager;
private const string DisplayName = "Test Job";       

public SyncHelper()
{
    manager = new BitsManager();
}        

BitsJob uploadBitsJob = manager.CreateJob(DisplayName, JobType.Upload);

I am getting following error :

A field initializer cannot reference the non-static field, method, or property 'BITSIntegrationModule.SyncService.SyncHelper.manager'

Madurika Welivita
  • 890
  • 1
  • 10
  • 19

2 Answers2

11

The line

BitsJob uploadBitsJob = manager.CreateJob(DisplayName, JobType.Upload);

can't access manager because it hasn't been set to anything yet - you could move the allocation into the constructor -

private readonly BitsManager manager;
private const string DisplayName = "Test Job";       
BitsJob readonly uploadBitsJob;

public SyncHelper()
{
  manager = new BitsManager();
  uploadBitsJob = manager.CreateJob(DisplayName, JobType.Upload);
}   
NDJ
  • 5,189
  • 1
  • 18
  • 27
  • I would also add readonly on uploadBitsJob to make sure it can't be changed once object is created. If it makes sense. – GregC Mar 04 '13 at 15:05
  • Do you ask me to move `BitsJob uploadBitsJob = manager.CreateJob(DisplayName, JobType.Upload);` part into constructor as well. B'cos `manager = new BitsManager();` is already within the constructor. – Madurika Welivita Mar 04 '13 at 15:06
  • I've updated with a bit more code - I meant to allocate uploadBitsJob in the constructor - it can't be initialised how you're currently doing it because manager isn't set until the constructor – NDJ Mar 04 '13 at 15:07
  • @NDJ yes, moving into constructor works fine..But still I am little bit confuse about what happen, if you can explain this little more for me. thanks. – Madurika Welivita Mar 04 '13 at 15:10
  • If you were setting it to say a new BitsJob, then it would be fine - BitsJob uploadBitsJob = new BitsJob(); But, because it needs manager to be set then you need to defer the allocation until manager refers to something valid – NDJ Mar 04 '13 at 15:12
  • @MadurikaWelivita - the rules of C# say that you're not allowed to access any instance methods/fields/properties when you're using a field initializer. That's what the error says. That's what you've got to avoid doing. It's tricky to know how else to say it. – Damien_The_Unbeliever Mar 04 '13 at 15:15
  • @NDJ thanks, so, why is it not enough to keep only ` manager = new BitsManager();` within the constructor? – Madurika Welivita Mar 04 '13 at 15:16
  • As Damien_The_Unbeliever says, because uploadBitsJob can't be instantiated with a reliance on an instance method/field/property - and to create your uploadBitsJob you need the manager instance method CreateJob. – NDJ Mar 04 '13 at 15:19
3

uploadBitsJob is declared at the class level which makes it a field. Field instances can't be used to initialize other fields.

Instead, you can declare the field without initializing it:

BitsJob uploadBitsJob;

Then initialize the field in the constructor:

public SyncHelper()
{
  manager = new BitsManager();
  uploadBitsJob = manager.CreateJob(DisplayName, JobType.Upload);//here.  Now manager is initialized
}  
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348