AnonDCX AnonDCX - 1 month ago 7
C# Question

Singleton: Form instance returning null

I have a windows Forms application with one form and a few classes.

I want to get the values of some textBoxes from the Form1 instance and extract the values.

My first way of achieving this was by using

Application.OpenForms[]
array to get the form but I realised that using a singleton on the class Form1 would be more efficient as I can have direct access and it would be impossible to make other instances.

Here is how I have set it up:

1. Controls class to gets controls from 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. Singleton set up inside class Form1

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;
}


As you can probably see I am getting the "NullReferenceException" when I attempt to get the Singleton from class Form1.

The following methods I have used are as follows:


  • Using Windows.OpenForms["Form1"].Controls["--somecontrol--"]

  • Using Windows.ActiveForm

  • Using a Singleton Design Pattern on class Form1



All of these ways are returning null and I cant think of a reason why it is returning null.

Any help would be appreaciated.

Thankyou

Answer

I want to get the values of some textBoxes from the Form1 instance and extract the values.

This is where you need to stop and re-think your approach. Forms represent views of your data; however, your data itself needs to be in the model, a separate place independent of the views.

Text boxes need to reflect the state of some model object, such as a Person object that has string properties for employer, company, role, web site, and so on. The form would read from that object's properties, display them in a text box, and then react to text box changes, and save values back to the model Person object.

If you make Person a singleton, or provide some other universal way of accessing it, you would be able to access person's properties from all forms, without accessing the forms themselves.