My assignment is to implement dynamic array which grows by one and preserves the values.
I'm creating a windows form program on visual studio and I need to increment array size by one every time btnRuaj is clicked then output all array values on a label on form,I created the array emertimi outside the btnRuaj method and I incremented the array size with counter variable everytime that button is clicked but when I try to output them on label produkti1 only the actual value is printed,the values are not saved. Example: when I try to output only the element 0 from array it prints it only for the first time and other times is null. This is the code:
public partial class Form1 : Form
{
string[] emertimi;
double[,] rekordetFinanciare;
int counter;
public Form1()
{
InitializeComponent();
}
private void btnRuaje_Click(object sender, EventArgs e)
{
if (counter >= 5)
{
MessageBox.Show("not enough space");
}
else
{
counter++;
emertimi = new string[counter];
emertimi[counter - 1] = textBox1.Text;
}
}
private void btnRekordet_Click(object sender, EventArgs e)
{
produkti1.Text = emertimi[0];
}
}
that's because everytime emertimi = new string[counter]; executes the array is reinitialises and set to empty,I suggest you to use list because the length of list is not fixed and it's much simpler
since you have to use array this is my way to do it:
it's not the best way but for a homework it's good enough