Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

A field initializer cannot reference the nonstatic field, method, or property but already initialized in ctor

i couldn’t use _cloudinaryConfig because of this error:

A field initializer cannot reference the nonstatic field, method, or property

I’m not sure what supposed to do

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 private IAppRepository _appRepository;
 private IMapper _mapper;
 private IOptions<CloudinarySettings> _cloudinaryConfig;

 public PhotosController(
        IAppRepository appRepository,
        IMapper mapper,
        IOptions<CloudinarySettings> cloudinaryConfig)
 {
     this._appRepository = appRepository;
     this._mapper = mapper;
     this._cloudinaryConfig = cloudinaryConfig;
 }

 string cloud = _cloudinaryConfig.Value.CloudName;
 string apiKey = _cloudinaryConfig.Value.ApiKey;
 string apiSecret = _cloudinaryConfig.Value.ApiSecret;

 Cloudinary _cloudinary = new Cloudinary(new Cloudinary(
        new Account(cloud, apiKey, apiSecret)));

>Solution :

You mean something like this?

 private IAppRepository _appRepository;
 private IMapper _mapper;
 private IOptions<CloudinarySettings> _cloudinaryConfig;

 string cloud;
 string apiKey;
 string apiSecret;

 Cloudinary _cloudinary;

 public PhotosController(
        IAppRepository appRepository,
        IMapper mapper,
        IOptions<CloudinarySettings> cloudinaryConfig)
 {
     this._appRepository = appRepository;
     this._mapper = mapper;
     this._cloudinaryConfig = cloudinaryConfig;
     this.cloud = _cloudinaryConfig.Value.CloudName;
     this.apiKey = _cloudinaryConfig.Value.ApiKey;
     this.apiSecret = _cloudinaryConfig.Value.ApiSecret
     this._cloudinary = new Cloudinary(new Cloudinary(
        new Account(cloud, apiKey, apiSecret)));
 }

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading