单例:表单实例返回 null
Singleton: Form instance returning null
我有一个 windows 表单应用程序,其中包含一个表单和几个 classes。
我想从 Form1 实例中获取一些文本框的值并提取这些值。
我实现此目的的第一种方法是使用 Application.OpenForms[]
数组来获取表单,但我意识到在 class Form1 上使用单例会效率更高,因为我可以直接访问并且不可能创建其他实例。
这是我的设置方式:
1.控件class从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; } }
}
}
2.里面设置单例classForm1
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;
}
正如您可能看到的那样,当我尝试从 class Form1.
获取 Singleton 时,我得到了 "NullReferenceException"
我用过的方法如下:
- 使用 Windows.OpenForms["Form1"].Controls["--somecontrol--"]
- 使用Windows.ActiveForm
- 在 class Form1
上使用单例设计模式
所有这些方法都返回 null,我想不出返回 null 的原因。
如有任何帮助,我们将不胜感激。
谢谢
I want to get the values of some textBoxes from the Form1 instance and extract the values.
这是您需要停下来重新考虑您的方法的地方。表单代表您数据的 视图 ;但是,您的数据本身需要位于 模型 中,这是一个独立于视图的独立位置。
文本框需要反映某些模型对象的状态,例如具有雇主、公司、角色、网站等字符串属性的 Person
对象。表单将从该对象的属性中读取,将它们显示在文本框中,然后对文本框的更改做出反应,并将值保存回模型 Person
对象。
如果您将 Person
设为单例,或提供其他通用访问方式,您将能够从所有表单访问人员的属性,而无需访问表单本身。
我有一个 windows 表单应用程序,其中包含一个表单和几个 classes。
我想从 Form1 实例中获取一些文本框的值并提取这些值。
我实现此目的的第一种方法是使用 Application.OpenForms[]
数组来获取表单,但我意识到在 class Form1 上使用单例会效率更高,因为我可以直接访问并且不可能创建其他实例。
这是我的设置方式:
1.控件class从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; } }
}
}
2.里面设置单例classForm1
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;
}
正如您可能看到的那样,当我尝试从 class Form1.
获取 Singleton 时,我得到了 "NullReferenceException"我用过的方法如下:
- 使用 Windows.OpenForms["Form1"].Controls["--somecontrol--"]
- 使用Windows.ActiveForm
- 在 class Form1 上使用单例设计模式
所有这些方法都返回 null,我想不出返回 null 的原因。
如有任何帮助,我们将不胜感激。
谢谢
I want to get the values of some textBoxes from the Form1 instance and extract the values.
这是您需要停下来重新考虑您的方法的地方。表单代表您数据的 视图 ;但是,您的数据本身需要位于 模型 中,这是一个独立于视图的独立位置。
文本框需要反映某些模型对象的状态,例如具有雇主、公司、角色、网站等字符串属性的 Person
对象。表单将从该对象的属性中读取,将它们显示在文本框中,然后对文本框的更改做出反应,并将值保存回模型 Person
对象。
如果您将 Person
设为单例,或提供其他通用访问方式,您将能够从所有表单访问人员的属性,而无需访问表单本身。