Я создал список, основанный на struct, но не могу изменить значение элемента. Поэтому я использовал класс вместо struct, но проблема, когда я напечатал список, он дал мне последний элемент, вставленную дубликатом в соответствии с количеством элементов. < /p>
Например. Если я вставлю "a", "b", "c", то вывод будет C C C C не B C < /p>
Вот код: < /p>
public struct Item //this is working fine but can't change item price
{
public string Code { get; set; }
public string Description{ get; set; }
public string Price{ get; set; }
public string Qty { get; set; }
}
< /code>
public static Class Item //this is not working it's overwrite the last value
{
public string Code { get; set; }
public string Description{ get; set; }
public string Price{ get; set; }
public string Qty { get; set; }
}
< /code>
Rest of code
public static Item xItem = new Item();
public static List item = new List();
xItem.Code = txtCode.Text;
xItem.Description = txtDescription.text;
xItem.Price= txtPrice.text;
xItem.Qty = txtQty.text;
< /code>
Я попробовал оба этого (дает тот же результат) < /p>
item.Insert(i,xItem);
// and
item.Add(xItem);
< /code>
в btnsave_click < /code> Я добавляю это < /p>
foreach (var s in item)
{
System.Diagnostics.Debug.WriteLine(s.Code +" \t " + s.Qty);
}
Подробнее здесь: https://stackoverflow.com/questions/515 ... in-c-sharp