简体   繁体   中英

Object is instantiated but still getting Warning: “Field 'x' is never assigned to …”

Description

I think I declare and instantiate the class: _teamsVM, but I keep getting the waring, quote:

Field 'StartpageVM2._teamsVM' is never assigned to, and will Always have its default value null.

Also : when running the application, it indeed gives me an error that this class is not instantiated (proving VS2017 is somehow right).

Question What am I missing??

Environment:

  • Microsoft Visual Studio 2017
  • Type of project: Universal Windows

Code

Here below is the code of the 4 classes involved.

The base class viewmodel:

using System.ComponentModel;

namespace MyApp.ViewModels
{
    public class BaseViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }
}

The main Viewmodel (containing the warning):

using System.Collections.ObjectModel;
using MyApp.Models;

namespace MyApp.ViewModels
{

    public class StartPageVM2 : BaseViewModel
    {
        #region Declarations
        private TeamsVM2 _teamsVM;
        public ObservableCollection<Team2> Teams {
            get { return _teamsVM.Teams; }
            set { _teamsVM.Teams = value; }
        }
        #endregion Declarations

        #region Constructor
        public StartPageVM2()
        {
            TeamsVM2 _teamsVM = new TeamsVM2();
        }
        #endregion
    }
}

The sub viewmodel:

using MyApp.Models;
using System.Collections.ObjectModel;

namespace MyApp.ViewModels
{
    public class TeamsVM2 : BaseViewModel
    {
        private ObservableCollection<Team2> _teams;
        public ObservableCollection<Team2> Teams {
            get { return _teams; }
            set { _teams = value; }
        }

        public TeamsVM2()
        {
            ObservableCollection<Team2> _teams = new ObservableCollection<Team2>();
        }
    }
}

And the Model class used:

namespace MyApp.Models
{
    public class Team2
    {
        private string _sTeamName;
        public string TeamName {
            get { return _sTeamName; }
            set { _sTeamName = value; }
        }

        public Team2() { }
        public Team2(string sTeamName)
        {
            _sTeamName = sTeamName;
        }
    }
}

In your Constructor you never assign the member variable but create a new variable within the scope of the constructor:

#region Constructor
public StartPageVM2()
{
    TeamsVM2 _teamsVM = new TeamsVM2();
}
#endregion

beware the scope. at that point _teamsVM != this._teamsVM

Do following instead:

#region Constructor
public StartPageVM2()
{
    _teamsVM = new TeamsVM2(); //or write  this._teamsVM = new TeamsVM2(); to highlight the scope
}
#endregion

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM