×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Update element value in a List (of Class)

Update element value in a List (of Class)

Update element value in a List (of Class)

(OP)
Hi Folks

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

CODE --> C#

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 --> C#

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 --> C#

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?

RE: Update element value in a List (of Class)

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

RE: Update element value in a List (of Class)

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

RE: Update element value in a List (of Class)

(OP)
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!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close