Reputation: 1828
OK, so I have learned how to create a list, view items in the list, and use the items in the list. I now want to learn how to edit the information that is in the list.
Here is my list:
class ObjectProperties
{
public string ObjectNumber { get; set; }
public string ObjectComments { get; set; }
public string ObjectAddress { get; set; }
}
List<ObjectProperties> Properties = new List<ObjectProperties>();
This is how I am adding values to the list:
ObjectProperties record = new ObjectProperties
{
ObjectNumber = txtObjectNumber.Text,
ObjectComments = txtComments.Text,
ObjectAddress = addressCombined,
};
Properties.Add(record);
I am wanting the user to enter which number they want to update by using a textbox(txtUpdateObjectNumber). I then want to compare that number to the values that are stored in record.ObjectNumber and then if it exist I want to replace the information in record.ObjectNumber and record.ObjectComments where record.ObjectNumber == txtUpdateObjectNumber. If you need me to elaborate on anything just let me know. Any help would be appreciated. Thank You :)
Upvotes: 1
Views: 1246
Reputation: 13360
To find the list item, use linq:
ObjectProperties opFound = Properties.Find(x => x.ObjectNumber == txtUpdateObjectNumber.Text);
Or the delegate form:
ObjectProperties opFound = Properties.Find(delegate (ObjectProperties x) { return x.ObjectNumber == txtUpdateObjectNumber.Text; });
Once you've found the item in the list, any changes you make to opFound
, including the ObjectNumber
, will persist in the list.
Upvotes: 2