Solved

Rookie question from java programmer

Posted on 2011-03-23
9
308 Views
Last Modified: 2012-05-11
so i am trying to do a basic C# windows application.
I have a form that calls another form (Modal)
The settings form needs to access some vars from the parent form
So i am not sure the best way , but i thought i would try this::

           SettingsForm settingsForm = new SettingsForm();
            settingsForm.setParent(this);
            settingsForm.ShowDialog();

then in the settings form i tried::

       public void setParent(Form1 form1)
        {
            this.parent = form1;
        }

        public SettingsForm()
        {
            InitializeComponent();
            tbServerURL.Text = parent.ServerURL;
            tbDatFileLocation.Text = parent.DataSourceLocation;
        }



When i call the method that opens the modal form i get the error:::

System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.


I am sure this is some very rookie misunderstanding of the language.
Any help would be appreciated

Thanks


0
Comment
Question by:paries
  • 4
  • 3
  • 2
9 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35202310
The constructor for your settings form:
public SettingsForm()
        {
            InitializeComponent();
            tbServerURL.Text = parent.ServerURL;
            tbDatFileLocation.Text = parent.DataSourceLocation;
        }

Open in new window


Is executed when it's instantiated with the SettingsForm settingsForm = new SettingsForm() line, and is making references to parent before your call to setParent.

Try passing the reference to the parent in the SettingsForm's constructor.

SettingsForm settingsForm = new SettingsForm(this);
settingsForm.ShowDialog();

Open in new window


public class SettingsForm : Form
{
  private Form1 parent;
  public SettingsForm(Form1 parent)
  {
    InitializeComponent();
    this.parent = parent;
    tbServerURL.Text = parent.ServerURL;
    tbDatFileLocation.Text = parent.DataSourceLocation;
  }
}

Open in new window

0
 
LVL 7

Expert Comment

by:jdavistx
ID: 35202363
The reason you received that error is because you're setting the parent after the new Settings form has already been instantiated.  Try this instead:

//In Form1
SettingsForm settingsForm = new SettingsForm();
settingsForm.ShowDialog();

//In SettingsForm
public SettingsForm(Form1 form1)
{
	InitializeComponent();
	this.parent = form1;
	tbServerURL.Text = parent.ServerURL;
}

Open in new window


However, passing the entire form object may not be the most practical solution to retrieve the values.  A common method for using "global" variables is to create a static class, and use it for your variables that will be used across different forms.

http://msdn.microsoft.com/en-us/library/79b3xss3(v=VS.100).aspx

0
 
LVL 7

Expert Comment

by:jdavistx
ID: 35202372
Ah, I forgot the form in the SettingsForm(this) call, but tgerbert's comment has it in there.
0
 

Author Comment

by:paries
ID: 35202376
thats for the response

that is what i did when i originally called settingsForm.Show(this);

but when i changed to  settingsForm.ShowDialog(this); (cause i want it to be modal)

it still only calls public SettingsForm()

I set my breakpoint in both
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 333 total points
ID: 35202461
Instantiating the form (SettingsForm settingsForm = new SettingsForm(this)) and calling it's ShowDialog method (settingsForm.ShowDialog()) are two distinct, separate steps.

public partial class Form1 : Form
{
	public Form1()
	{
		InitializeComponent();
	}

	private void ShowSettingsFormButton_Click(object sender, EventArgs e)
	{
		// Declare a variable of type "SettingsForm" named "settingsFrm"
		SettingsForm settingsFrm;

		// Instantiate it - create an instance of the SettingsForm class and assign the
		// resulting object to the settingsFrm variable, pass "this" to SettingsForm's constructor
		settingsFrm = new SettingsForm(this);

		// Call the ShowDialog on the instance of the settings form
		settingsFrm.ShowDialog();
	}
}

Open in new window


Pass "this" in the SettingsForm's constructor, not as a parameter to one of it's methods.
0
 
LVL 7

Accepted Solution

by:
jdavistx earned 167 total points
ID: 35202483
Show vs ShowDialog wouldn't matter for your purposes.

//This is my default constructor!
public SettingsForm()
{
	InitializeComponent();
}

//This is my reference to the parent form, but I'm not initialized to anything yet
//If some other method tried to use this.parent before passing Form1 to SettingsForm then it would throw an error because
//SettingsForm doesn't know anything about Form1 yet.
private Form1 parent;

//This is my constructor that's used when a Form1 is passed
public SettingsForm(Form1 form)
{
	InitializeComponent();
	//Using tgerbert's suggestion, you would have a private member that will be your reference to the parent form
	//Now I have a copy of the form and I can use values from the form assuming they have the proper accessibility set (e.g. public, internal, protected, etc)
	this.parent = form;
}

public void SomeSettingMethod()
{
	MySettingTextBox.Text = this.parent.SomeParentTextBox.Text;
}

Open in new window


0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 333 total points
ID: 35202506
@jdavistx

You need to type faster. ;)
0
 

Author Closing Comment

by:paries
ID: 35202518
thanks
0
 
LVL 7

Expert Comment

by:jdavistx
ID: 35202599
@tgerbert
Sorry, people keep coming by my office with questions!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now