?
Solved

Rookie question from java programmer

Posted on 2011-03-23
9
Medium Priority
?
351 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

765 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