Click here to Skip to main content
15,906,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have have a string array:
C#
public class Example
{
    public string[] Layers = new string[] { };

    public Example()
    {
        //Just to be safe...
        this.Layers = new string[] { };
    }
}

and whenever I try to access a member of that string array:
C#
Example test = new Example();
test.Layers[Convert.ToInt32(numericUpDown1.Value)] = textbox1.Text;
it always gives me an IndexOutOfRange exception. Why is this happening and how can I fix it? Because logically it should work?.. and also can someone please answer at where an array starts? Is it 1 or 0?

Thanks Jake
Posted

You need to mention the size of the array

C#
public class Example
   {
       public string[] Layers = new string[7] ;

       public Example()
       {
           //Just to be safe...
           this.Layers = new string[7] ;
       }
   }

Morevover Array index starts from 0 in C#

Have a look at this

http://stackoverflow.com/questions/1603599/array-of-string-with-unknown-size[^]
 
Share this answer
 
v2
According to you code there are no members (it's a zero length array).
You need something like this:
C#
this.Layers = new string[] { "a", "b", "c" };
 
Share this answer
 

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



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