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];
    }
}

1 Answers

0
ehsan kiani On

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

public partial class Form1 : Form
{

     List<string> emertimi=new List<string>();
    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.Add(textBox1.Text);

        }
    }
    private void btnRekordet_Click(object sender, EventArgs e)
    {
        produkti1.Text =emertimi[emertimi.Count - 1];
    }
}

since you have to use array this is my way to do it:

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
    {
     string[] tmp= new string[counter];
        foreach (var i=0;i<emertimi.length;i++ )
        { 
           tmp[i]=emertimi[i];
        }
        counter++;
        emertimi = new string[counter];
        emertimi[counter - 1] = textBox1.Text;
         foreach (var i=0;i<tmp.length;i++ )
        { 
           emertimi[i]=tmp[i];
        }
    }
}
private void btnRekordet_Click(object sender, EventArgs e)
{
    produkti1.Text = emertimi[0];
}
}

it's not the best way but for a homework it's good enough