C# Singleton:返回null的表单实例
我有一个windows窗体应用程序,有一个窗体和几个类 我想从Form1实例中获取一些文本框的值,并提取这些值 我实现这一点的第一种方法是使用C# Singleton:返回null的表单实例,c#,winforms,singleton,instance,C#,Winforms,Singleton,Instance,我有一个windows窗体应用程序,有一个窗体和几个类 我想从Form1实例中获取一些文本框的值,并提取这些值 我实现这一点的第一种方法是使用Application.OpenForms[]array获取表单,但我意识到在类Form1上使用单例将更有效,因为我可以直接访问,并且不可能生成其他实例 以下是我如何设置它的: 1。Controls类从Form1 class Controls { //Request Form1 instance private static Form1 f
Application.OpenForms[]
array获取表单,但我意识到在类Form1上使用单例将更有效,因为我可以直接访问,并且不可能生成其他实例
以下是我如何设置它的:
1。Controls类从Form1
class Controls
{
//Request Form1 instance
private static Form1 form = Form1.GetInstance();
//Sets global values for easy access with getters and null setters
//--Variable 'form' is still null hence I get the NullReferenceException
private TextBox employer = form.Controls["textBoxEmployerName"] as TextBox;
private TextBox role = form.Controls["textBoxRole"] as TextBox;
private TextBox company = form.Controls["textBoxCompanyName"] as TextBox;
private TextBox website = form.Controls["textBoxWebsite"] as TextBox;
private TextBox refNumber = form.Controls["textBoxRefNumber"] as TextBox;
private TextBox reason = form.Controls["textBoxReason"] as TextBox;
private TextBox dateListed = form.Controls["textBoxDateListed"] as TextBox;
private Label charLimit = form.Controls["labelCharsRemaining"] as Label;
public TextBox Employer { get { return employer; } }
public TextBox Role { get { return role; } }
public TextBox Company { get { return company; } }
public TextBox Website { get { return website; } }
public TextBox RefNumber { get { return refNumber; } }
public TextBox Reason { get { return reason; } }
public TextBox DateListed { get { return dateListed; } }
public Label CharLimit { get { return charLimit; } }
}
}
public partial class Form1 : Form
{
private static Form1 theInstance;
public Form1()
{
InitializeComponent();
}
//Return instance of Form1
//--This is obviously returning null for some reason
public static Form1 GetInstance()
{
if (theInstance == null)
theInstance = new Form1();
return theInstance;
}
2.在班级内设置单身学生表格1
class Controls
{
//Request Form1 instance
private static Form1 form = Form1.GetInstance();
//Sets global values for easy access with getters and null setters
//--Variable 'form' is still null hence I get the NullReferenceException
private TextBox employer = form.Controls["textBoxEmployerName"] as TextBox;
private TextBox role = form.Controls["textBoxRole"] as TextBox;
private TextBox company = form.Controls["textBoxCompanyName"] as TextBox;
private TextBox website = form.Controls["textBoxWebsite"] as TextBox;
private TextBox refNumber = form.Controls["textBoxRefNumber"] as TextBox;
private TextBox reason = form.Controls["textBoxReason"] as TextBox;
private TextBox dateListed = form.Controls["textBoxDateListed"] as TextBox;
private Label charLimit = form.Controls["labelCharsRemaining"] as Label;
public TextBox Employer { get { return employer; } }
public TextBox Role { get { return role; } }
public TextBox Company { get { return company; } }
public TextBox Website { get { return website; } }
public TextBox RefNumber { get { return refNumber; } }
public TextBox Reason { get { return reason; } }
public TextBox DateListed { get { return dateListed; } }
public Label CharLimit { get { return charLimit; } }
}
}
public partial class Form1 : Form
{
private static Form1 theInstance;
public Form1()
{
InitializeComponent();
}
//Return instance of Form1
//--This is obviously returning null for some reason
public static Form1 GetInstance()
{
if (theInstance == null)
theInstance = new Form1();
return theInstance;
}
正如您可能看到的,当我尝试从类Form1获取单例时,我得到了“NullReferenceException”
我使用的方法如下:
- 使用Windows.OpenForms[“Form1”].Controls[“--somecontrol--”]
- 使用Windows.ActiveForm
- 在类Form1上使用单例设计模式
Person
对象。表单将读取该对象的属性,在文本框中显示它们,然后对文本框的更改做出反应,并将值保存回modelPerson
对象
如果您将
Person
设置为单例,或提供其他通用的访问方式,则可以从所有表单访问Person的属性,而无需访问表单本身。确定您所说的MVC方法吗?@AnonDCX是的,没错。它不需要使用MVC框架或类似的东西,只需要MVC所暗示的关注点分离。谢谢,我将重新思考并实现一些关注点分离原则。您确定表单
为空吗?如果强制转换不起作用,as
操作符将返回null,我非常确定form.Controls[“nonExistentasdf323”]
也将返回null
。