Andreas Jung

Andreas Jung

  • 1.6k
  • 9
  • 706

How to change Items in a c-sharp list

Jun 1 2024 11:51 AM

How to change Items in a c-sharp list.

I learn now data bindingbasics and I am confronted with c-sharp list-class

I have the following list class declaration

List<Class2> raceCarDrivers = new List<Class2>();

            //Populate list data source with data items
            this.raceCarDrivers.Add(new Class2("M. Schumacher", 500));
            this.raceCarDrivers.Add(new Class2("R. Schumacher", 501));
            this.raceCarDrivers.Add(new Class2("A. Senna", 502));
            this.raceCarDrivers.Add(new Class2("A. Prost", 503));
            this.raceCarDrivers.Add(new Class2("F. Schoebel", 504));

            //I want to change the second property (Wins) of M. Schumacher:
            ++this.raceCarDrivers.Wins;
            this.winsTextBox.Text = raceCarDrivers.Wins.ToString();*/  

//But I get he error message: System.Collection.Generics.List<DataBindingBasics01.Class2> does not contain a definition for "Wins" and no extension method "Wins" could be found that accepts a first argument of type "System.Collections.Generic.List<DataBindingBasics01.Class2>. (is a listing directive or assembly reference missing?)

the class2 is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace DataBindingBasics01
{
    class Class2
    {
        string name;
        int wins;

        public Class2(string name, int wins)
        {
            this.name = name;
            this.wins = wins;
        }

        //event
        public event PropertyChangedEventHandler PropertyChanged;

        public string Name
        {
            get { return this.name; }
            set { this.name = value;
            this.OnPropertyChanged("Name");
            }
        }
        public int Wins
        {
            get { return this.wins; }
            set { this.wins = value;
            this.OnPropertyChanged("Wins");
            }
        }

        //helper from the event
        void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                //this = object sender
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

When I click to "Add Win" button there should be one more win for M. Schumacher


Answers (2)