如何修复数组错误? [英] How do I fix an array error?

查看:74
本文介绍了如何修复数组错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个字符串数组:

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" };


这篇关于如何修复数组错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆