Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Rookie question from java programmer

Posted on 2011-03-23
9
Medium Priority
?
354 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

610 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