代码如下:
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];
}
}写好代码后,点击生成-重新生成解决方案,编译完成后,就会提示错误:“引用封送类的字段,访问上面的成员可能导致运行时异常。”如下图:
请问各位大侠,该如何解决这个问题?
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];
}
}写好代码后,点击生成-重新生成解决方案,编译完成后,就会提示错误:“引用封送类的字段,访问上面的成员可能导致运行时异常。”如下图:
请问各位大侠,该如何解决这个问题?
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!");
}
}
确实没有错误提示了,谢谢大侠!