0

I could not figure it out how to update an item of a generic list after review all the questions posted here, I'm sorry for that. Here is my question:

I have this structure:

List<LineInfo> Lines = new List<LineInfo>();
    LineInfo LInfo;
    struct LineInfo
    {
        public int line;
        public int WO;
        public string Brand;
        public string Model;
        public string Customer;
        public int Qty;
        public int Target;
        public string Leader;
        public string Start;
        public string End;
        public int Percent;
    }      

And I want to update the field "Percent" of one of the LInfo items entered, I have the current position (aCurrentLine).

LInfo.Percent = Convert.ToInt32((RealProduced / DesireProd) * 100);                 
Lines[aCurrentLine]....?

Please advise, thanks.

3 Answers 3

3

Just

LInfo.Percent = Convert.ToInt32((RealProduced / DesireProd) * 100);
Lines[aCurrentLine] = LInfo;

should work... but please don't use either public fields or mutable structs. Both are terrible in terms of maintainability and unintended effects.

Most of the types you create in C# are likely to be classes - it's relatively rare that you'll want to create a value type (struct). You should make sure you're aware of the differences between the two.

Likewise fields in C# should almost always be private. They should be an implementation detail of the type, not part of its public API. Use properties instead - automatically-implemented properties in C# 3 make these almost as compact to write as fields, if you just want a trivial property.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! thank you, and regarding mutable structs, I need to read about it ;)
1

I have just one adivice .Mutable stuctures are evil. Try to avoid it .

Lines[aCurrentLine] = LInfo;

you wont be able to access Lines[aCurrentLine].Percent as it updates just a temporary copy.

Comments

0

for update a generic list record with grid view.just put this code.

 List<Address> people = (List<Address>)Session["People"];
        people[e.RowIndex].DoorNo = ((TextBox)grdShow.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        people[e.RowIndex].StreetName = ((TextBox)grdShow.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        people[e.RowIndex].City = ((TextBox)grdShow.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
        people[e.RowIndex].PhoneNo = ((TextBox)grdShow.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
        grdShow.EditIndex = -1;
        grdShow.DataSource = people;
        grdShow.DataBind();

and put this code to page load event.

if (!IsPostBack)
        {

            List<Address> people = new List<Address>();
            Session["People"] = people;
        }

for create generic list using grid view write this code on the button event(it take the data from text boxes and save in the list)

GenerateList();//call the method GenerateList();

the sytex of GenerateList();

private void GenerateList()
    {
        if (Session["People"] != null)
        {

            List<Address> people = (List<Address>)Session["People"];

            Address a1 = new Address();

            a1.DoorNo = txtDoorno.Text;
            a1.StreetName = txtStreetName.Text;
            a1.City = txtCityname.Text;
            a1.PhoneNo = txtPhoneno.Text;
            people.Add(a1);
            Session["People"] = people;
            grdShow.DataSource = people;
            grdShow.DataBind();
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.