日期:2014-05-17  浏览次数:20992 次

关于代码规范的问题,求解决办法......
代码如下:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test_Static
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            for (int i = 0; i < 3; i++)
            {
                testlabel.LabelBelt[i] = new MyLabel();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            testlabel.LabelBelt[0].OpenTime_Full  = DateTime.Now.AddHours(3);
            testlabel.LabelBelt[0].StartLocation = 123;

            if (testlabel.LabelBelt[0].OpenTime_Full.CompareTo(DateTime.Now) > 0)
            {
                MessageBox.Show("大于当前时间!");
            }
            if (testlabel.LabelBelt[0].StartLocation.ToString().Trim() == "123")
            {
                MessageBox.Show("文本是123!");
            }
        }
    }

    public class MyLabel : Label
    {
        public int StartLocation;

        public DateTime OpenTime_Full;

    }

    public static class testlabel
    {
        public static MyLabel[] LabelBelt = new MyLabel[3];
    }
}





写好代码后,点击生成-重新生成解决方案,编译完成后,就会提示错误:“引用封送类的字段,访问上面的成员可能导致运行时异常。”如下图:




请问各位大侠,该如何解决这个问题?
------最佳解决方案--------------------
http://technet.microsoft.com/zh-cn/library/x524dkh4(de-de).aspx
------其他解决方案--------------------
按照楼上的大侠的提示,把代码改为:


private void button1_Click(object sender, EventArgs e)
        {