tags:

views:

177

answers:

5

I'm trying to make a dynamic array in C# but I get an annoying error message. Here's my code:

    private void Form1_Load(object sender, EventArgs e)
    {
        int[] dataArray;
        Random random = new Random();
        for (int i = 0; i < random.Next(1, 10); i++)
        {
            dataArray[i] = random.Next(1, 1000);
        }
    }

And the error:

Use of unassigned local variable 'dataArray'

This is just baffling my mind. I came from VB, so please me gentle, lol.

Cheers.

+25  A: 

You haven't created the array - you've declared the variable, but not given it a value.

Note that arrays always have a fixed size. If you want a data structure that you can just keep adding to, you should use List<T>. However, I'd advise working out the size once rather than on each iteration through the loop. For example:

private void Form1_Load(object sender, EventArgs e)
{
    List<T> dataArray = new List<T>();
    Random random = new Random();
    int size = random.Next(1, 10);
    for (int i = 0; i < size; i++)
    {
        dataArray.Add(random.Next(1, 1000));
    }
}

Of course if you're working out the size beforehand, you can use an array after all:

private void Form1_Load(object sender, EventArgs e)
{
    Random random = new Random();
    int size = random.Next(1, 10);
    int[] dataArray = new int[size];
    for (int i = 0; i < size; i++)
    {
        dataArray[i] = random.Next(1, 1000);
    }
}

... but be aware that arrays are considered somewhat harmful.

Jon Skeet
I was 1/2 through my answer when I noticed this. This is the best answer. Also as another alternative, you could use ArrayList in place of List<int>. However, List<int> is the best choice if only ints are going into the Array.
galford13x
And also, when using `List<T>` often one can hint about the initial capacity which makes adding items to the list faster.
Peter Lillevold
ArrayList is highly not recommended since we have generics in .NET 2.0+. Boxing and unboxing makes ArrayList a bad choice.
Lex Li
+1  A: 

You have to initialize the array. If you are wanting something dynamic you need to use a List object.

Bryan Denny
A: 
    int[] dataArray= new int[10];
    Random random = new Random();
    for (int i = 0; i < random.Next(1, 10); i++)
    {
        dataArray[i] = random.Next(1, 1000);
    }
ydobonmai
+1  A: 

Looks like you need to initialize the dataArray.

Do int[] dataArray = new int[10] instead of just the code you have there at the first line of the method.

where 10 is the amount of elements you'll be dealing with.

If you're not sure of the size, you'd be better off using an ArrayList or better yet, a List.

Mikeyg36
In his case, 10 would be the *maximum* number of elements, since he's trying to set size between [1,10]
JeffH
A: 

Try This:

        Random random = new Random();
        int cnt = random.Next(1, 10);
        int[] dataArray = new int[cnt];
        for (int i = 0; i < cnt; i++)
        {
            dataArray[i] = random.Next(1, 1000);
        } 
Vivek