Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update element value in a List (of Class) 1

Status
Not open for further replies.

Amesville

Programmer
Oct 10, 2011
93
US
Hi Folks

OK I've taken Rhys advice and created a class in my program as follows:

Code:
    public class CarMovement
    {
        public int Id { get; set; }
        public string CarName { get; set; }
        public string MoveStatus { get; set; }
        public string SourceCtrl { get; set; }
        public string DestCtrl { get; set; }
    }

And I create an instance of it to put in the values, then assign it to the list:

Code:
        List<CarMovement> Movement = new List<CarMovement>();
                            
                            int iID;
                            iID = Movement.Count();
                            iID++;
                            CarMovement Move = new CarMovement();
                            Move.Id = iID;
                            Move.CarName = sCarName;
                            Move.MoveStatus = "Begin";
                            Move.SourceCtrl = sCtrlName;
                            Movement.Add(Move);

Great. So now here I am, adding things to the list and now I need to go back and update something I already added. I can find the index of the necessary list item this way:

Code:
            int Idx = Movement.FindIndex(item => item.SourceCtrl == txt.Name);
            if (Idx >= 0)
            {...}

What I can't figure out is how to update any of the elements (like CarName) in the listitem. I have the index but nothing I'm trying seems to allow me to change an element within the list. Any ideas?
 
Just off of your code please note if (Idx >= -1), the indexing on the list will be zero based. Anyway, how about using the Find method instead, something along the lines of;
Code:
CarMovement carMovement = Movement.Find(item => item.SourceCtrl.Equals(txt.Name, StringComparison.CurrentCultureIgnoreCase));
if (carMovement != null)
{
    carMovement.CarName = @"Old Banger";
}


Rhys

"Technological progress is like an axe in the hands of a pathological criminal"
"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
Albert Einstein
 
Ignore first sentence of my reply... it was early, I was tired and looking at three things at once... ahem [dazed]

Rhys

"Technological progress is like an axe in the hands of a pathological criminal"
"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
Albert Einstein
 
Thanks Rhys. I very much appreciate the help. This list stuff is pretty new to me but I can see that it's pretty powerful. I just wish more of the online resources were clearer!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top