I want to transfer an array from Form1 to Form2, then on Form2 add some values to the array. On Form1 button click I put this code:
int[] arrayOfInt
Don't use arrays if you want a data structure that you need to add items to.
Use a generic collection like List
In your case, a list of integers would be a List
.
IList listOfInt = new List();
listOfInt.Add(19);
listOfInt.Add(12);
Form2 frm2 = new Form2();
frm2.TakeThis(listOfInt);
frm2.Show();
When on Form2
, your TakeThis
function would look like this:
public voidTakeThis(IList listOfInt)
{
listOfInt.Add(34);
}
This will also work when passing the list to another form, as List
is a reference type whereas arrays are value types. If you don't know what this means, see this article.