Solved

Rookie question from java programmer

Posted on 2011-03-23
9
332 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

791 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