0

I am struggling to add an element into an array from a text box using C# Windows Forms. hers what I have so far:

int[] id;

private void btnButton1_Click(object sender, EventArgs e)
    {
        //INSERTION SORT
        int newItem = txtAddElement.text;

        //CODE HERE TO ADD ELEMENT TO ARRAY


        //CODE BELOW THEN SORTS ARRAY INTO CORRECT ORDER

        int element;
        int temp;

        for (int i = 1; i < id.Length; i++)
        {
            element = i - 1;

            while (element >= 0 && id[element] > id[element + 1])
            {
                temp = id[element];
                id[element] = id[element + 1];
                id[element + 1] = temp;
            }
        }

        for (int i = 1; i < id.Length; i++)
        {
            lstPlayers.Items.Add(id[i]);
        }

        txtAddElement.Text = "";
    }

I know this insertion sort works because I have manually added some values in previously, however the basic part now seems to be tripping me up.

What I want is for the program to run with an empty array, as coded above, when I enter a value into txtAddElement I want to use a button btnAddToArray to insert this value into the array. For example:

if i type 12 into txtAddElement, and then press btnAddToArray, i would like the array to now have 1 item of 12, If was was to then add another number via the txtAddElement, lets say 7, and press the btnAddToArray button, I want the array to then have 2 values [12, 7] once I have mastered this then alls i need to do is add the insertion sort to this.

error:

CODE SNIPPET

   int[] id;

    private void btnLogOn_Click(object sender, EventArgs e)
    {
        Array.Resize(ref id, id.Length + 1); //Object reference not set to an instance of an object.
        id[id.Length - 1] = Convert.ToInt16(txtLogOn.Text);

        //INSERTION SORT
        int element;
        int temp;

SOLVED:

  int[] id = new int[0];
5
  • 1
    why dont you use a List<int> instead? Commented Nov 25, 2015 at 12:29
  • Ok if button is clicked then this handler is invoked and sort works but it doesn't take any values from textbox directly. So where is the 'textbox part' of your code ? Commented Nov 25, 2015 at 12:31
  • the question I have been asked says use an array, otherwise I would have no issues with this Commented Nov 25, 2015 at 12:32
  • i am using the btnAddToArray to control the text box, once the button is pressed, I want this button to add the item into the array, and then it will empty the textbox Commented Nov 25, 2015 at 12:33
  • @dragonAMC show this code please as it is relevant Commented Nov 25, 2015 at 12:34

1 Answer 1

3

You can't add to array. You should either use List<T> e.g.

  List<int> id;
  ...

  id.Add(123);

Or re-size the array (not recommended)

  int[] id;
  ... 
  Array.Resize(ref id, id.Length + 1);
  id[id.Length - 1] = 123;
Sign up to request clarification or add additional context in comments.

6 Comments

(In general) Why wouldn't it be recommended to re-size the array ?
@Иво Недев: it could be resource consuming - array (unlike List<T>) is designed as constant size collection.
Because this logic is already implemented for you in Lists. And it's very likely that you'll make bugs while implementing what is already done.
each time I run the program the array will be null, so the array size isnt going to be that great, I have to show more that I can do it rather than whats the most efficient was. thankyou for your help, and I shall be using Lists normally from now on!!
ahh when adding in the resize it states "object reference not set to an instance of an object" see above
|

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.