读书人

关于代码规范的有关问题求解决方法.

发布时间: 2012-12-16 12:02:32 作者: rapoo

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


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)
{
testlabel.LabelBelt[0].OpenTime_Full = DateTime.Now.AddHours(3);


testlabel.LabelBelt[0].StartLocation = 123;

DateTime z = testlabel.LabelBelt[0].OpenTime_Full;

if (z.CompareTo(DateTime.Now) > 0)
{
MessageBox.Show("大于当前时间!");
}

int k = testlabel.LabelBelt[0].StartLocation;

if (k.ToString().Trim() == "123")
{
MessageBox.Show("文本是123!");
}
}



确实没有错误提示了,谢谢大侠!

读书人网 >C#

热点推荐