Mar*_*lor 1 c# oop methods class
我有一个类,有一个方法,当没有被调用导致崩溃.
有没有办法在不调用方法时使编译失败?
编辑:
所以我所拥有的基本上是一个类,它可以创建另一个类(一个表单),它是一个带有不再显示选项的信息框.这是一个如何使用它的例子.
public partial class Form1 : Form {
DontShowAgainBox box;
public void AlertYes() {
if (box.form.showagain.Checked)
t1.Text = "You chose yes (checked)!!!";
else
t1.Text = "You chose yes (unchecked)!!!";
}
public void AlertNo() {
if (box.form.showagain.Checked)
t1.Text = "You chose no (checked)!!!";
else
t1.Text = "You chose no (unchecked)!!!";
}
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
box = new DontShowAgainBox("Warning", "Are you sure?");
Action yesaction = new Action(AlertYes);
box.Bind_Yes(yesaction);
Action noaction = new Action(AlertNo);
box.Bind_No(noaction);
box.SetNoButton("Nope");
box.SetYesButton("I'm sure");
box.Show();
}
}
Run Code Online (Sandbox Code Playgroud)
还有另一种方法可以隐藏"否"按钮.
但是yes按钮是在类的每个实例中,因此它需要有一个与之关联的函数,否则......崩溃.
为什么不在软件中测试而不是崩溃呢?
if (!init_called) {
print error
exit
}
Run Code Online (Sandbox Code Playgroud)