none
In c# how do I initialize an interface field in a class RRS feed

  • Question

  • Hello,

    I'm using Visual Studio 2017 Community.

    I have a class defined like this:

    namespace Nop.Plugin.Misc.LakesideImport.ExportImport
    {
        /// <summary>
        /// Import manager
        /// </summary>
        public class LakesideImportManager : ImportManager
        {
              #region Fields
    
              private readonly IProductService _productService;
              private readonly IProductAttributeService _productAttributeService;
              private readonly ISpecificationAttributeService _specificationAttributeService;

    I'm getting a warning on "_specificationAttributeService" that says: "Field 'LakesideImportManager._specificationAttributeService' is never assigned to, and will always have it's default value null"

    How do I initialize "_specificationAttributeService"?

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.


    • Edited by Carneno Monday, September 25, 2017 7:28 PM
    Monday, September 25, 2017 7:24 PM

Answers

  • Well it looks to me based on all the IWhatevers being dependency injected via the class' constructor, that an IoC like Unity or one of the other IoC(s) is instancing all objects, and that is where you have to address getting your Interface with its class defined so it can be instanced as an object.

    If you have all of the source code, then search the code for SpecificationAttributeService and see what has reference to it where you may see a configuration for it in an ioC for it in an IoC container.

    I was able to resolve this issue by adding this line to the fields in my class:

    private readonly ISpecificationAttributeService _specificationAttributeService;


    Adding this to the parameter list of the constructor in my class:

    ISpecificationAttributeService specificationAttributeService


    And adding this to the constructor body (between the braces):

    this._specificationAttributeService = specificationAttributeService;

    Thanks for everybodys help.

    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.


    • Marked as answer by Carneno Saturday, September 30, 2017 7:04 PM
    • Edited by Carneno Saturday, September 30, 2017 7:16 PM
    Saturday, September 30, 2017 7:04 PM

All replies

  • Wouldn't you want to set the value in your class, but make it ready only to other classes?

    public ISpecificationAttributeService SpecificationAttributeService {get; private set;}

    Monday, September 25, 2017 8:55 PM
  • Wild Bill,

    Thanks for your help.

    I don't think I have the object oriented programming knowledge to answer your question.

    "SpecificationAttributeService" is a partial class in another name space that has a virtual method that I would like to use in my class.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Monday, September 25, 2017 9:12 PM
  • So in your constructor, you could

    public LakesideImportManager()
    {
       this.SpecificationAttributeService = new someothernamespace.SpecificationAttributeService();
    }
    
    public void MethodTest()
    {
       this.SpecificationAttributeService.SomeVirtualMethod();
    }

    Monday, September 25, 2017 10:57 PM
  • Wild Bill,

    Thanks for your help.

    I'm assuming that when you say "in your constructor", you mean I put that statement between the body braces of the constructor.

    I put it in this way:

    this.SpecificationAttributeService = new Nop.Services.Catalog.SpecificationAttributeService();
    

    It gives an error when I hover over "SpecificationAttributeService": "'LakesideImportManager' does not contain a definition for 'SpecificationAttributeService' and no extension method, etc."

    It gives another error when I hover over "Nop.Services.Catalog.SpecificationAttributeService": "There is no argument given that corresponds to the required formal parameter 'cacheManager' of 'SpecificationAttributeService.SpecificationAttributeService'(ICacheManager ... and other fields)'".

    Do I need to put in all of the parameters that SpecificationAttributeService is looking for in it signature?

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Tuesday, September 26, 2017 7:33 PM
  • Do I need to put in all of the parameters that SpecificationAttributeService is looking for in it signature?

    Yes
    Tuesday, September 26, 2017 9:55 PM
  • Wild Bill,

    It still gives the same error for the part before the "= new" and after the "= new", it says "'SpecificationAttributeService' does not contain a constructor that takes 7 arguments."

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Tuesday, September 26, 2017 10:09 PM
  • The problem is I don't have an example of the class you're trying to initialize.  If it is saying a constructor doesn't exist for 7 arguments, then you'll have to add/remove arguments to match what's in the signature of that constructor.  Is ImportManager a class or an interface?  If it is a class, does it have an instance of SpecificationAttributeService exposed? 
    Wednesday, September 27, 2017 11:04 PM
  • How do I initialize "_specificationAttributeService"?

    private readonly ISpecificationAttributeService_specificationAttributeService

    The above Interface represents the public properties and or methods of a class that exposing them to the class that is using the Interface. 

    The  above is set by the instance of the class that is instanced into an object.

    1) The Interface is set by passing the object in on the class's constructor.

    2) The Interface is set by passing the object in on the class's public property

    3) The Interface is set by instancing by the 'new' keyword of the object.  

    Thursday, September 28, 2017 12:15 AM
  • Wild Bill,

    I would like to give you the code that is involved in what I am trying to accomplish.

    My class, LakesideImportManager, inherits ImportManager, which looks like this:

    namespace Nop.Services.ExportImport
    {
        /// <summary>
        /// Import manager
        /// </summary>
        public partial class ImportManager : IImportManager
        {
            #region Fields
    
            private readonly IProductService _productService;
            private readonly IProductAttributeService _productAttributeService;
            private readonly ICategoryService _categoryService;
            private readonly IManufacturerService _manufacturerService;
            private readonly IPictureService _pictureService;
            private readonly IUrlRecordService _urlRecordService;
            private readonly IStoreContext _storeContext;
            private readonly INewsLetterSubscriptionService _newsLetterSubscriptionService;
            private readonly ICountryService _countryService;
            private readonly IStateProvinceService _stateProvinceService;
            private readonly IEncryptionService _encryptionService;
            private readonly IDataProvider _dataProvider;
            private readonly MediaSettings _mediaSettings;
            private readonly IVendorService _vendorService;
            private readonly IProductTemplateService _productTemplateService;
            private readonly IShippingService _shippingService;
            private readonly IDateRangeService _dateRangeService;
            private readonly ITaxCategoryService _taxCategoryService;
            private readonly IMeasureService _measureService;
            private readonly CatalogSettings _catalogSettings;
            private readonly IProductTagService _productTagService;
            private readonly IWorkContext _workContext;
            private readonly ILocalizationService _localizationService;
            private readonly ICustomerActivityService _customerActivityService;
            private readonly VendorSettings _vendorSettings;
    
            #endregion
    
            #region Ctor
    
            public ImportManager(IProductService productService,
                ICategoryService categoryService,
                IManufacturerService manufacturerService,
                IPictureService pictureService,
                IUrlRecordService urlRecordService,
                IStoreContext storeContext,
                INewsLetterSubscriptionService newsLetterSubscriptionService,
                ICountryService countryService,
                IStateProvinceService stateProvinceService,
                IEncryptionService encryptionService,
                IDataProvider dataProvider,
                MediaSettings mediaSettings,
                IVendorService vendorService,
                IProductTemplateService productTemplateService,
                IShippingService shippingService,
                IDateRangeService dateRangeService,
                ITaxCategoryService taxCategoryService,
                IMeasureService measureService,
                IProductAttributeService productAttributeService,
                CatalogSettings catalogSettings,
                IProductTagService productTagService,
                IWorkContext workContext,
                ILocalizationService localizationService,
                ICustomerActivityService customerActivityService,
                VendorSettings vendorSettings)
            {
                this._productService = productService;
                this._categoryService = categoryService;
                this._manufacturerService = manufacturerService;
                this._pictureService = pictureService;
                this._urlRecordService = urlRecordService;
                this._storeContext = storeContext;
                this._newsLetterSubscriptionService = newsLetterSubscriptionService;
                this._countryService = countryService;
                this._stateProvinceService = stateProvinceService;
                this._encryptionService = encryptionService;
                this._dataProvider = dataProvider;
                this._mediaSettings = mediaSettings;
                this._vendorService = vendorService;
                this._productTemplateService = productTemplateService;
                this._shippingService = shippingService;
                this._dateRangeService = dateRangeService;
                this._taxCategoryService = taxCategoryService;
                this._measureService = measureService;
                this._productAttributeService = productAttributeService;
                this._catalogSettings = catalogSettings;
                this._productTagService = productTagService;
                this._workContext = workContext;
                this._localizationService = localizationService;
                this._customerActivityService = customerActivityService;
                this._vendorSettings = vendorSettings;
            }
    
            #endregion
    
            #region Utilities


    My class looks like this:

    namespace Nop.Plugin.Misc.LakesideImport.ExportImport
    {
        public class LakesideImportManager : ImportManager
         {
              #region Fields
    
              private readonly IProductService _productService;
              private readonly IProductAttributeService _productAttributeService;
              private readonly ICategoryService _categoryService;
              private readonly IManufacturerService _manufacturerService;
              private readonly IPictureService _pictureService;
              private readonly IUrlRecordService _urlRecordService;
              private readonly IStoreContext _storeContext;
              private readonly INewsLetterSubscriptionService _newsLetterSubscriptionService;
              private readonly ICountryService _countryService;
              private readonly IStateProvinceService _stateProvinceService;
              private readonly IEncryptionService _encryptionService;
              private readonly IDataProvider _dataProvider;
              private readonly MediaSettings _mediaSettings;
              private readonly IVendorService _vendorService;
              private readonly IProductTemplateService _productTemplateService;
              private readonly IShippingService _shippingService;
              private readonly IDateRangeService _dateRangeService;
              private readonly ITaxCategoryService _taxCategoryService;
              private readonly IMeasureService _measureService;
              private readonly CatalogSettings _catalogSettings;
              private readonly IProductTagService _productTagService;
              private readonly IWorkContext _workContext;
              private readonly ILocalizationService _localizationService;
              private readonly ICustomerActivityService _customerActivityService;
              private readonly VendorSettings _vendorSettings;
    
              //private readonly ISpecificationAttributeService _specificationAttributeService;
    
              #endregion
    
              #region Ctor
    
              public LakesideImportManager(IProductService productService,
                ICategoryService categoryService,
                IManufacturerService manufacturerService,
                IPictureService pictureService,
                IUrlRecordService urlRecordService,
                IStoreContext storeContext,
                INewsLetterSubscriptionService newsLetterSubscriptionService,
                ICountryService countryService,
                IStateProvinceService stateProvinceService,
                IEncryptionService encryptionService,
                IDataProvider dataProvider,
                MediaSettings mediaSettings,
                IVendorService vendorService,
                IProductTemplateService productTemplateService,
                IShippingService shippingService,
                IDateRangeService dateRangeService,
                ITaxCategoryService taxCategoryService,
                IMeasureService measureService,
                IProductAttributeService productAttributeService,
                // ISpecificationAttributeService specificationAttributeService,
                CatalogSettings catalogSettings,
                IProductTagService productTagService,
                IWorkContext workContext,
                ILocalizationService localizationService,
                ICustomerActivityService customerActivityService,
                VendorSettings vendorSettings) : base (productService,
                     categoryService,
                     manufacturerService,
                     pictureService,
                     urlRecordService,
                     storeContext,
                     newsLetterSubscriptionService,
                     countryService,
                     stateProvinceService,
                     encryptionService,
                     dataProvider,
                     mediaSettings,
                     vendorService,
                     productTemplateService,
                     shippingService,
                     dateRangeService,
                     taxCategoryService,
                     measureService,
                     productAttributeService,
                     catalogSettings,
                     productTagService,
                     workContext,
                     localizationService,
                     customerActivityService,
                     vendorSettings)
              {
                   this._productService = productService;
                   this._categoryService = categoryService;
                   this._manufacturerService = manufacturerService;
                   this._pictureService = pictureService;
                   this._urlRecordService = urlRecordService;
                   this._storeContext = storeContext;
                   this._newsLetterSubscriptionService = newsLetterSubscriptionService;
                   this._countryService = countryService;
                   this._stateProvinceService = stateProvinceService;
                   this._encryptionService = encryptionService;
                   this._dataProvider = dataProvider;
                   this._mediaSettings = mediaSettings;
                   this._vendorService = vendorService;
                   this._productTemplateService = productTemplateService;
                   this._shippingService = shippingService;
                   this._dateRangeService = dateRangeService;
                   this._taxCategoryService = taxCategoryService;
                   this._measureService = measureService;
                   this._productAttributeService = productAttributeService;
                   this._catalogSettings = catalogSettings;
                   this._productTagService = productTagService;
                   this._workContext = workContext;
                   this._localizationService = localizationService;
                   this._customerActivityService = customerActivityService;
                   this._vendorSettings = vendorSettings;
    
              }
    
              #endregion
    
              #region Methods


    The SpecificationAttributeService looks like this:

    namespace Nop.Services.Catalog
    {
        /// <summary>
        /// Specification attribute service
        /// </summary>
        public partial class SpecificationAttributeService : ISpecificationAttributeService
        {
            #region Constants
    
            /// <summary>
            /// Key for caching
            /// </summary>
            /// <remarks>
            /// {0} : product ID
            /// {1} : allow filtering
            /// {2} : show on product page
            /// </remarks>
            private const string PRODUCTSPECIFICATIONATTRIBUTE_ALLBYPRODUCTID_KEY = "Nop.productspecificationattribute.allbyproductid-{0}-{1}-{2}";
            /// <summary>
            /// Key pattern to clear cache
            /// </summary>
            private const string PRODUCTSPECIFICATIONATTRIBUTE_PATTERN_KEY = "Nop.productspecificationattribute.";
            #endregion
    
            #region Fields
            
            private readonly IRepository<SpecificationAttribute> _specificationAttributeRepository;
            private readonly IRepository<SpecificationAttributeOption> _specificationAttributeOptionRepository;
            private readonly IRepository<ProductSpecificationAttribute> _productSpecificationAttributeRepository;
            private readonly ICacheManager _cacheManager;
            private readonly IEventPublisher _eventPublisher;
    
            #endregion
    
            #region Ctor
    
            /// <summary>
            /// Ctor
            /// </summary>
            /// <param name="cacheManager">Cache manager</param>
            /// <param name="specificationAttributeRepository">Specification attribute repository</param>
            /// <param name="specificationAttributeOptionRepository">Specification attribute option repository</param>
            /// <param name="productSpecificationAttributeRepository">Product specification attribute repository</param>
            /// <param name="eventPublisher">Event published</param>
            public SpecificationAttributeService(ICacheManager cacheManager,
                IRepository<SpecificationAttribute> specificationAttributeRepository,
                IRepository<SpecificationAttributeOption> specificationAttributeOptionRepository,
                IRepository<ProductSpecificationAttribute> productSpecificationAttributeRepository,
                IEventPublisher eventPublisher)
            {
                _cacheManager = cacheManager;
                _specificationAttributeRepository = specificationAttributeRepository;
                _specificationAttributeOptionRepository = specificationAttributeOptionRepository;
                _productSpecificationAttributeRepository = productSpecificationAttributeRepository;
                _eventPublisher = eventPublisher;
            }
    
            #endregion
    
            #region Methods


    One of the methods in SpecificationAttributeService that I want to use looks like this:

            /// <summary>
            /// Gets specification attributes
            /// </summary>
            /// <param name="pageIndex">Page index</param>
            /// <param name="pageSize">Page size</param>
            /// <returns>Specification attributes</returns>
            public virtual IPagedList<SpecificationAttribute> GetSpecificationAttributes(int pageIndex = 0, int pageSize = int.MaxValue)
            {
                var query = from sa in _specificationAttributeRepository.Table
                            orderby sa.DisplayOrder, sa.Id
                            select sa;
                var specificationAttributes = new PagedList<SpecificationAttribute>(query, pageIndex, pageSize);
                return specificationAttributes;
            }


    ImportManager does not have an instance of SpecificationAttributeService.

    I hope all this helps in trying to figure this out.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.


    • Edited by Carneno Thursday, September 28, 2017 12:22 PM
    Thursday, September 28, 2017 12:20 PM
  • ImportManager does not have an instance of SpecificationAttributeService.

    I hope all this helps in trying to figure this out.

    Where did you get this code from? It looks to be like an IoC is being used with constrictor injection being used with the IoC controlling object instancing.   
    Thursday, September 28, 2017 12:43 PM
  • DA,

    LakesideImportManager is what i wrote.

    The rest of the code is from here.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Thursday, September 28, 2017 12:57 PM
  • DA,

    LakesideImportManager is what i wrote.

    The rest of the code is from here.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Well it looks to me based on all the IWhatevers being dependency injected via the class' constructor, that an IoC like Unity or one of the other IoC(s) is instancing all objects, and that is where you have to address getting your Interface with its class defined so it can be instanced as an object.

    If you have all of the source code, then search the code for SpecificationAttributeService and see what has reference to it where you may see a configuration for it in an ioC for it in an IoC container.

    Thursday, September 28, 2017 1:37 PM
  • Wild Bill,

    Thanks for your help.

    I don't think I have the object oriented programming knowledge to answer your question.

    "SpecificationAttributeService" is a partial class in another name space that has a virtual method that I would like to use in my class.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Hi Carneno,

    If I may suggest, this is a thin, quick read book that will add a lot to your programming skills.

    I suggest you get a copy and refer to it frequently:

    Design Patterns: Elements of Reusable Object-Oriented Software


    My Technet Articles

    If you like this or another reply, vote it up!
    If you think this or another reply answers the original question, mark it or propose it as an answer.


    Mauricio Feijo
    www.mauriciofeijo.com

    Thursday, September 28, 2017 2:02 PM
  • Well it looks to me based on all the IWhatevers being dependency injected via the class' constructor, that an IoC like Unity or one of the other IoC(s) is instancing all objects, and that is where you have to address getting your Interface with its class defined so it can be instanced as an object.

    If you have all of the source code, then search the code for SpecificationAttributeService and see what has reference to it where you may see a configuration for it in an ioC for it in an IoC container.

    I was able to resolve this issue by adding this line to the fields in my class:

    private readonly ISpecificationAttributeService _specificationAttributeService;


    Adding this to the parameter list of the constructor in my class:

    ISpecificationAttributeService specificationAttributeService


    And adding this to the constructor body (between the braces):

    this._specificationAttributeService = specificationAttributeService;

    Thanks for everybodys help.

    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.


    • Marked as answer by Carneno Saturday, September 30, 2017 7:04 PM
    • Edited by Carneno Saturday, September 30, 2017 7:16 PM
    Saturday, September 30, 2017 7:04 PM