How do I copy data from one list to another so that they are independent of one another? I have a < > of a class that I created where the data inserted in it, will appear in a listBox, and wanted to copy everything that is in that list to a new one. I managed to copy everything from one to another, but when I work with the second list, the original (first list) undergoes unwanted changes.
This is my class:
public class vao
{
public int quantidade { get; set; }
public double medida { get; set; }
}
This is how I am inserting the data into the list and making it displayed in the listbox.
List<vao> vaos = new List<vao>();
List<vao> ordenada = new List<vao>();
private void button1_Click(object sender, EventArgs e)
{
vao A = new vao();
A.quantidade = Convert.ToInt32(textBox1.Text);
A.medida = Convert.ToDouble(textBox2.Text);
vaos.Add(A);
listBox1.Items.Clear();
foreach (vao item in vaos)
{
listBox1.Items.Add(item.quantidade + " x " + item.medida);
}
textBox1.Text = "";
textBox1.Focus();
textBox2.Text = "";
}
And this form is how I am copying everything from a list (vaos) to the list (sorted) and to display what is in the (sorted) list in a new listbox in the way that you would like.
private void button3_Click(object sender, EventArgs e)
{
ordenada = vaos;
for (int i = 0; i <= ordenada.Count - 1; i++)
{
for (int j = i + 1; j < ordenada.Count; j++)
{
if (ordenada[i].medida < ordenada[j].medida)
{
int aux_qt = ordenada[i].quantidade;
ordenada[i].quantidade = ordenada[j].quantidade;
ordenada[j].quantidade = aux_qt;
Double aux_med = ordenada[i].medida;
ordenada[i].medida = ordenada[j].medida;
ordenada[j].medida = aux_med;
}
}
}
listBox2.Items.Clear();
foreach (var item in ordenada)
{
listBox2.Items.Add(item.quantidade + " x " + item.medida);
}
}
InthiscaseIenteredthequantitiesandmeasurementsandcopiedandsorted.
WhenIselectanindexfromtheOriginallist,thevaluestheyhavearetheonesthatareintheSortlist