'C# appending values to empty array int[] doesn't work
I'm doing a windows form app and I need to declare empty array for some listbox operations. But i can't add value to array. When I try to print the lenght of the array after adding a value I reading the value 0 all the time.
public partial class Form1 : Form
{
public static int[] arrayOfNumbers = new int[] { };
public Form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
arrayOfNumbers.Append(Convert.ToInt32(textBox1.Text));
Console.WriteLine(arrayOfNumbers.Length);
}
Solution 1:[1]
Enumerable.Append
appends an item to a sequence and returns it, so you need to use it to re-assign a new array to the field. But it doesn't return an arrray but IEnumerable<T>
, so you have to append ToArray
:
arrayOfNumbers = arrayOfNumbers.Append(Convert.ToInt32(textBox1.Text)).ToArray();
Arrays are a fixed size collection which you can't modify anyway.
Solution 2:[2]
Besides Tim Schmelter's answer being absolutely correct, what the OP most apparently wanted to do is using a List<int>
and its Add
method instead of an array1.
Append
is a so called LINQ method, which would return a "copy"2 of the array with that value appended, not modifying the instance you called it on, which is not what was intended to happen.
public partial class Form1 : Form
{
public static List<int> arrayOfNumbers = new(); // use a List<int>
public Form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
arrayOfNumbers.Add(Convert.ToInt32(textBox1.Text)); // extend instance with Add
Console.WriteLine(arrayOfNumbers.Count); // use List's Count property
}
1 I suppose the OP has a Python background, since Python "lists" look like C# arrays due to the square brackets, and do not have a fixed size. The C# companion to Python lists are List
instances, however, with slightly different method names like Add
instead of Append
.
2 This statement is extremely simplified since LINQ is lazy-evaluated, only returning an enumerator with the effects you want it to have once you iterated through it, never modifying the instance you call it on or creating new instances by itself.
Solution 3:[3]
public void Submit_Click(object sender, RoutedEventArgs e)
{
ListUser[] Acts = {}; //Empty string array
int length = Acts.Length; //get array length to int
int ActInt = length; //assign to separate int var
//--if--//
//Check if array is 0 length and append
//values from two textboxes to the Acts
//array.
//Get new length of Acts array and assign
//to int length and int ActInt.
//Then create an array to hold a copy of
// the values using correct length
//Bind Acts array to ListView DataSource
//--else--//
//If Acts array is not empty "0" write
//vals to console. AKA Append again.
if (ActInt==0)
{
Acts = Acts.Append(new ListUser(NameEntry.Text, TimeEntry.Text)).ToArray();
length = Acts.Length;
ActInt = length;
ListUser[] ActsOld = new ListUser[ActInt];
System.Array.Copy(Acts, ActsOld, ActInt);
ActsList.ItemsSource=Acts;
}
else if (Acts!=0)
{
/*Append to array here and remove foreach*/
//--|--//
//--V--//
foreach (var i in Acts)
{
Console.WriteLine(i);
}
}
}
Where ListUser[] Is A Second Class File To Define The Attributes Of The Array
public class ListUser
{
public string lName { get; set; }
public string lTime { get; set; }
public ListUser(string lname, string ltime)
{
lName = lname;
lTime = ltime;
}
}
Thank You for Your Help Tim your answer utilizing;
arr = arr.Append(element(params)).ToArray();
Worked perfectly.
If anybody could advise on a quick and dirty way to click and drag to reorder entries within a ListView / GridView any help would be great!!
<ListView x:Name="ActsList">
<ListView.View>
<GridView x:Name="ActsListGrid">
<GridView.Columns>
<GridViewColumn Width="300"
Header="Name"
DisplayMemberBinding="{Binding lName}" />
<GridViewColumn Width="80"
Header="time"
DisplayMemberBinding="{Binding lTime}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tim Schmelter |
Solution 2 | |
Solution 3 | Halbach Audio |