locked
Why Object can't be find it?

    Question

  • Hi,

    I have 2 classes.

    The line           
    return Foods.First(x => x._id == id);

    Gives me an error
    Error 1 'Check_Eat.Food' does not contain a definition for '_id' and no extension method '_id' accepting a first argument of type 'Check_Eat.Food' could be found (are you missing a using directive or an assembly reference?) 

    What I am doing wrong?
    Thanks


    namespace Check_Eat.Models
    {
        public class Food : INotifyPropertyChanged
        {
         
            #region Properties
            private int id;
            public int _id
            {
                get
                {
                    return id;
                }
                set
                {
                    if (id != value)
                    {
                        id = value;
                        OnNotifyPropertyChanged("_id");
                    }
                }
            }
           
            private string _code = string.Empty;
            public string code
            {
                get
                { return _code; }

                set
                {

                    if (_code != value)
                    {
                        _code = value;
                        OnNotifyPropertyChanged("code");
                    }
                }
            }

            private string _category = string.Empty;
            public string category
            {
                get
                { return _category; }

                set
                {
                    if (_category != value)
                    {
                        _category = value;
                        OnNotifyPropertyChanged("category");
                    }
                }
            }

            private string _description = string.Empty;
            public string description
            {
                get
                { return _description; }

                set
                {
                    if (_description != value)
                    {
                        _description = value;
                        OnNotifyPropertyChanged("description");
                    }
                }
            }

            private string _calories = string.Empty;
            public string calories
            {
                get
                { return _calories; }

                set
                {
                    if (_calories != value)
                    {
                        _calories = value;
                        OnNotifyPropertyChanged("calories");
                    }
                }
            }

            private string _serving = string.Empty;
            public string serving
            {
                get
                { return _serving; }

                set
                {
                    if (_serving != value)
                    {
                        _serving = value;
                        OnNotifyPropertyChanged("serving");
                    }
               
                }
            }

            #endregion "Properties"

            #region INotifyPropertyChanged Interface
            public event PropertyChangedEventHandler PropertyChanged;

            private void OnNotifyPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            #endregion
        }
    }


    namespace Check_Eat.ViewModels
    {
        public class FoodViewModel : INotifyPropertyChanged
        {
            #region Fields and Properties

            // This property will be bound to GridView's ItemsDataSource property for providing data
            private ObservableCollection<Food> foods;

            // This property will be bound to button's Command property for deleting item
            public IDelegateCommand DeleteCommand { protected set; get; }

            public ObservableCollection<Food> Foods
            {
                get
                {
                    return foods;
                }
                set
                {
                    if (foods != value)
                    {
                        foods = value;
                        OnPropertyChanged("Foods");
                    }
                }
            }

            #endregion

            #region Constructor
            public FoodViewModel()
            {
                // create a DeleteCommand instance
                this.DeleteCommand = new DelegateCommand(ExecuteDeleteCommand);

                // Get data source
                Foods = InitializeSampleData.GetData();
            }
            #endregion

            #region Execute and CanExecute methods
           void ExecuteDeleteCommand(object param)
            {
                int id = (Int32)param;
                Food cus = GetFoodById(id);
                if (cus != null)
                {
                    Foods.Remove(cus);
                }
            }

            #endregion
            // Get the deleting item by Id property
            private Food GetFoodById(int id)
            {
                return Foods.First(x => x._id == id);
            }
            #region INotifyPropertyChanged
            public event PropertyChangedEventHandler PropertyChanged;

            private void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            #endregion
        }
    }


    ADRIAN DIBU

    Monday, December 30, 2013 2:07 AM

Answers

  • Hi,

    Thanks for help.

    I found the mistake, no code error I used Food.cs and Food.xaml.cs names.

    Thanks


    ADRIAN DIBU

    • Marked as answer by adibu Monday, December 30, 2013 6:10 PM
    Monday, December 30, 2013 6:10 PM

All replies

  • is that the only error you get? it compiles on my machine

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, December 30, 2013 9:49 AM
  • Hi,

     Thank for the prompt answer.

    It is the only error

    1>------ Build started: Project: Check Eat, Configuration: Debug x86 ------

    error CS1061: 'Check_Eat.Food' does not contain a definition for '_id' and no extension method '_id' accepting a first argument of type 'Check_Eat.Food' could be found (are you missing a using directive or an assembly reference?)

    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    ========== Deploy: 0 succeeded, 0 failed, 0 skipped ==========

             
    return Foods.First(x => x._id == id);

    When I type . in x. I suppose to be able to see the in the list _id, code, category, description and serving

    It didn't happen.

    Thanks


    ADRIAN DIBU

    Monday, December 30, 2013 3:06 PM
  • can you share a complete project with us to show the problem? as said it works fine here with just the code from above

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, December 30, 2013 4:09 PM
  • Hi,

    Thanks for help.

    I found the mistake, no code error I used Food.cs and Food.xaml.cs names.

    Thanks


    ADRIAN DIBU

    • Marked as answer by adibu Monday, December 30, 2013 6:10 PM
    Monday, December 30, 2013 6:10 PM