Link to home
Start Free TrialLog in
Avatar of paries
paries

asked on

Rookie question from java programmer

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


Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

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

Avatar of jdavistx
jdavistx

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

Ah, I forgot the form in the SettingsForm(this) call, but tgerbert's comment has it in there.
Avatar of paries

ASKER

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
SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of paries

ASKER

thanks
@tgerbert
Sorry, people keep coming by my office with questions!