A field initializer cannot reference the non-static field, method, or property
Asked Answered
R

2

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'

Rosabelle answered 4/3, 2013 at 14:57 Comment(3)
And what part of the message do you have an issue with?Outside
The question would have been more clear had the OP included the class declaration for SyncHelper.Hanako
Don't forget to ask a question in your question. There's no question here, just a description of some behaviour.Inessential
L
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);
}   
Lamond answered 4/3, 2013 at 15:1 Comment(8)
I would also add readonly on uploadBitsJob to make sure it can't be changed once object is created. If it makes sense.Accent
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.Rosabelle
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 constructorLamond
@Lamond 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.Rosabelle
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 validLamond
@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.Outside
@Lamond thanks, so, why is it not enough to keep only ` manager = new BitsManager();` within the constructor?Rosabelle
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.Lamond
H
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
}  
Hanako answered 4/3, 2013 at 15:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.