Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Singleton:返回null的表单实例_C#_Winforms_Singleton_Instance - Fatal编程技术网

C# Singleton:返回null的表单实例

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

我有一个windows窗体应用程序,有一个窗体和几个类

我想从Form1实例中获取一些文本框的值,并提取这些值

我实现这一点的第一种方法是使用
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上使用单例设计模式
所有这些方法都返回null,我想不出返回null的原因

任何帮助都会得到报答

谢谢

我想从Form1实例中获取一些文本框的值,并提取这些值

这就是你需要停下来重新思考你的方法的地方。表单表示数据的视图;但是,数据本身需要位于模型中,独立于视图的单独位置

文本框需要反映某些模型对象的状态,例如具有雇主、公司、角色、网站等字符串属性的
Person
对象。表单将读取该对象的属性,在文本框中显示它们,然后对文本框的更改做出反应,并将值保存回model
Person
对象


如果您将
Person
设置为单例,或提供其他通用的访问方式,则可以从所有表单访问Person的属性,而无需访问表单本身。

确定您所说的MVC方法吗?@AnonDCX是的,没错。它不需要使用MVC框架或类似的东西,只需要MVC所暗示的关注点分离。谢谢,我将重新思考并实现一些关注点分离原则。您确定
表单
为空吗?如果强制转换不起作用,
as
操作符将返回null,我非常确定
form.Controls[“nonExistentasdf323”]
也将返回
null