Solved

Rookie question from java programmer

Posted on 2011-03-23
9
339 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

730 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