如何修复数组错误?
问题描述:
我有一个字符串数组:
Hi,
I have have a string array:
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:
Example test = new Example();
test.Layers[Convert.ToInt32(numericUpDown1.Value)] = textbox1.Text;
它总是给我一个IndexOutOfRange异常。为什么会发生这种情况,我该如何解决?因为从逻辑上讲它应该有用吗?..也有人可以在阵列开始的地方回答吗?它是1还是0?
谢谢Jake
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
答
你需要提一下数组的大小
You need to mention the size of the array
public class Example
{
public string[] Layers = new string[7] ;
public Example()
{
//Just to be safe...
this.Layers = new string[7] ;
}
}
Morevover数组索引从C开始0#
看看这个
http://stackoverflow.com/questions/1603599/array-of-string-with-unknown-size [ ^ ]
根据你的代码,没有成员(它是一个零长度的数组)。
你需要这样的东西:
According to you code there are no members (it's a zero length array).
You need something like this:
this.Layers = new string[] { "a", "b", "c" };