1

I have a following program, it takes strings from 4 textboxes and puts them into array and displays the message(created from the array) after user clicks on btnOK. I have a button called btnClear which if clicked should clear both the message string and array but it's not doing it and I'm not quite sure why, can someone have a look and advise.

string[,] namesTable = new string[10, 4];
int row = 0;
string message = "";

private void btnOK_Click_1(object sender, EventArgs e)
{

  namesTable[row, 0] = txtFirstName.Text;
  namesTable[row, 1] = txtSurname.Text;
  namesTable[row, 2] = txtPosition.Text;
  namesTable[row, 3] = txtComment.Text;
  row++;

  message = "Name.\tSurname\tPosition\tComment\n";
  for (int i = 0; i < namesTable.GetLength(0); i++)
  {
    if (namesTable[i, 0] != null)
    {
      for (int j = 0; j < namesTable.GetLength(1); j++)
      {
        message += namesTable[i, j] + "\t";
      }
      message += "\n";
    }
  }

  MessageBox.Show(message, "Names Table");
}

private void btnClear_Click_1(object sender, EventArgs e)
{
    Array.Clear(namesTable, 0, 4);
    message = "";
}

If you need more info please let me know.

3 Answers 3

1

The range of cleared elements wrap from row to row in a multi-dimensional array therefore You should use:

Array.Clear(namesTable, 0, 40);
//                      ^  ^
//                      |  |_The number of elements to clear.
//                      |
//                      |____The starting index of the range of elements to clear.
Sign up to request clarification or add additional context in comments.

2 Comments

@fadjad Can you explain, why when I have it set to 4 and do following: 1) enter 4 values, click OK, 2)click OK again 3)click Clear 4) click OK again Message box still shows second row and third?
Because in your code: Array.Clear(namesTable, 0, 4); only sets namesTable[0, 1] through namesTable[0, 4] zero.
0

If you want to clear all your data, so reinitialize all:

private void btnClear_Click_1(object sender, EventArgs e)
{
    this.namesTable = new string[10, 4];
    this.row = 0;
    this.message = "";
}

Comments

0

You are clearing 4 values in the array from index 0. However, your insert code inserts at the row index. This is likely to be your problem. What is the value of row ?

To clear everyting in the array, simply do:

namesTable = new string[10, 4];
row = 0;

4 Comments

row is automaticaly calculated - incremented when new "row" of data is created
Do you want to re-initalize all rows, or just the current one ? Under all circumstances, you will want to clear the correct portion of your array. If you need to clear everything just new a new array and set row to 0.
by "new a new array" do you mean clear existing one or something else?
Thank you it works too but I can only select one correct answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.