Click here to Skip to main content
14,694,553 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private const int id = 10;

        private string[] numArray = new string[id];
        private int index = 0;



          private void button1_Click(object sender, EventArgs e)
          {
              {
                  if (this.index < 10)
                  {
                      numArray[this.index] = textBox1.Text;
                      this.index++;
                  }
              }
          }

          private void button2_Click(object sender, EventArgs e)
          {
              for (int i = 0; i < 10; i++)
              {

                  listBox1.Items.Add(numArray[i]); //show array in a listbox

              }
          }

          private void button3_Click(object sender, EventArgs e)
          {
              textBox1.Clear();
          }
    }
}


Moved here from comment:

Um yeah actually I just saw that it works if I input exactly 10 strings but I want it to work if I input maybe even less than that..
That was why I was getting the error that it contains a null value
Posted
Updated 7-May-15 1:00am
v2
Comments
Sascha Lefèvre 7-May-15 5:26am
   
How does it not work?

The problem is that it does work: it is trying to do exactly what you told it too!

But...integers and strings are different: integers are Value types, and strings are Reference types.

So when you create an array of integers:
int[] myInts = new int[10];
You get an array of ten integers, filled with the default value, which is zero;
But the same code for strings:
string[] myStrings = new string[10];

Creates an array of ten references to strings, filled with teh defautl value for a Referance type, which is null

Try this instead:
for (int i = 0; i < index; i++)
    {
    listBox1.Items.Add(numArray[i]); //show array in a listbox
    }
   
To avoid the null-reference exception, initialize the array-indices in the constructor of your form with empty strings (after InitializeComponent()).

If you would use a List<string> instead of the array, you could circumvent that problem altogether, because you don't have to specify the size of the List beforehand and it will have exactly the size of how many strings you've added to it.

See:
https://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx[^]
http://www.dotnetperls.com/list[^]
   
Comments
Dakota335 7-May-15 6:24am
   
Thank you so much. It helped solve my problem
Sascha Lefèvre 7-May-15 6:26am
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900