日期:2014-05-18  浏览次数:21462 次

C#里面怎么定义,怎么使用全局数组?
代码如下,我使用了静态的类,MyLabel.MyLabelBelt是我自定义的控件,继承自label

C# code


namespace CarouselManager
{
    public static class testlabel
    {
        public static MyLabel.MyLabelBelt[] LabelBelt = new MyLabel.MyLabelBelt[1000];
    }


    public partial class View_Belt_Dyna 
    {
        

        public View_Belt_Dyna()
        {
            InitializeComponent();
            for (int i = 0; i < 1000; i++)
            {
                testlabel.LabelBelt[i] = new MyLabel.MyLabelBelt();
            }
            
        }

        private void simpleButton_Init_Click(object sender, EventArgs e)
        {
            for (int ii = 0; ii < 1000; ii++)
            {
                if (testlabel.LabelBelt[ii].Area.Trim() == "仓库1号")
                {
                    .........
                }
             }
        }
  
     }

}





编译可以通过,问题是,运行时停在这句上:if (testlabel.LabelBelt[ii].Area.Trim() == "仓库1号")

提示:未将对象引用设置到对象的实例。使用new关键字创建对象实例。

我就奇怪了,我已经在构造函数里面把所有数组成员都new了啊,怎么到这个simpleButton_Init_Click按钮响应函数里面又要再new呢?

求解答!谢谢!

------解决方案--------------------
如果构造函数执行了,其他地方又没有清空静态数组里面的对象的话,应该指的是Area为null吧
------解决方案--------------------
探讨

Area是一个自定义的属性,string类型的,难道这个属性也要New吗?