我想判断是否满足条件,如果不满足条件就不进行构造。代码如下:class TestForm
{
private static condition = false;
public TestForm()
{
if (condition)
{
InitializeComponent();
}
else
{
//......要如何放弃构造,代码如何写呢?
}
}
}谢谢~!
{
private static condition = false;
public TestForm()
{
if (condition)
{
InitializeComponent();
}
else
{
//......要如何放弃构造,代码如何写呢?
}
}
}谢谢~!
class TestForm
{
private static condition = false;
public TestForm()
{
if (condition)
{
InitializeComponent();
}
else
return;
}
} 其实直接把else后面的删掉也行吧,如果不满足条件就什么也不执行
{
private static bool condition = false;
public TestForm()
{
} public static TestForm CreateTestForm()
{
if (condition)
{
return new TestForm();
}
else
{
return null;
}
}
}
{
private static condition = false;
public TestForm()
{
if (condition)
{
InitializeComponent();
}
else
{
MessageBox.Show("没有构造");
this.Dispose();
}
}
} 自己试了一下,这样也可以
*****************************************************************************
QQ群交流:
dotnet 吧
67781963
*****************************************************************************
QQ群交流:
dotnet 吧
67781963
将代码发出来:大家使用命令行编译一下试试看看:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;public class MyForm:Form
{
private static bool condition = false;
public MyForm()
{
if (condition)
{
this.Height = 500;
this.Width = 500;
Button submitButton = new Button();
submitButton.Size = new Size(40, 50);
this.Controls.Add(submitButton);
condition = true;
}
else
{
return;
}
}
public static void Main()
{
Application.Run(new MyForm());
}
}
*****************************************************************************
QQ群交流:
dotnet 吧
67781963
你应该在外部对condition进行判断再决定要不要创建这个testForm对象
if(condition)
testForm form = new testForm;
不知你的意思是否是创建完后如果false的话就把这对象销毁呢?
那就this.Dispose();
但这对象是构造了又马上销毁,而不是放弃构造
{
InitializeComponent();
}
else
{
throw new ApplicationException();
}