Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

Passing information between forms

FormWITSMain has an object of type ClassXML named cxml.

FormWITSMain is opening form FormSetIPAndPort.

FormSetIPAndPort needs access to cxml (declared in FormWITSMain)

How does FormSetIPAndPort get to cxml?

==============================

NOTE:    Here is the constructor for FormWITSMain:

public FormWITSMain()
{
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();                  
}


My current invocation of FormSetIPAndPort from FormWITSMain looks like this:

private void menuItemSetIPandPort_Click(object sender, System.EventArgs e)
{
      FormSetIPAndPort fsipap = new FormSetIPAndPort(this);
      fsipap.ShowDialog();
}


and here is the constructor for FormSetIPAndPort:

public FormSetIPAndPort(FormWITSMain tempFWM)
{
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //

      fwm = tempFWM;
      this.textBoxIP.Text = fwm.cxml.UseIP;
      this.textBoxPort.Text = fwm.cxml.UsePort;
}


FormSetIPAndPort declares fwm as follows:

private FormWITSMain fwm = new FormWITSMain();




==============================




This actually seems to work just fine........but it really bugs me that I am calling the constructor for FormWITSMain several times.  There has got to be a better way than passing the entire FormWITSMain object around just to get access to a public data member (cxml).



Is there a way to get to the public members of FormWITSMain from another form without having to pass FormWITSMain to the other forms?
ASKER CERTIFIED SOLUTION
Avatar of BurntSky
BurntSky

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

ASKER

Also, there is no need to instantiate FormWITSMain from your second Form.  Doing that WILL create a whole new object.  Change the following line:

private FormWITSMain fwm = new FormWITSMain();

to

private FormWITSMain fwm;

================================================


Yes!!!!   This was what I was needing.

Thanks!!!


I have some questions on a few points of my code ..... can you verify that I understand it now....or am I still not getting it?




Here is my revised invocation of  FormSetIPAndPort from the main form FormWITSMain:


private void menuItemSetIPandPort_Click(object sender, System.EventArgs e)
{
      FormSetIPAndPort fsipap = new FormSetIPAndPort(this);  //I am passing a reference to FormWITSMain to the constructor for FormSetIPAndPort -- correct????

      fsipap.ShowDialog();
      System.Diagnostics.Debug.WriteLine("IP:  " + cxml.UseIP);
      System.Diagnostics.Debug.WriteLine("Port:  " + cxml.UsePort);
}



And here is my constructor in FormSetIPAndPort now:

private FormWITSMain fwm;   //Notice I am just creating the reference now, not instantiating a whole new object!!!!
            
public FormSetIPAndPort(FormWITSMain tempFWM)  //I still need to pass in the FormWITSMain object....right???????
{
      InitializeComponent();
      fwm = tempFWM;             ////////////////  here I am setting my FormWITSMain reference to the tempFWM object I passed into the constructor???
      this.textBoxIP.Text = fwm.cxml.UseIP;    //  now I have access to cxml so I can init the text values of the TextBox controls
      this.textBoxPort.Text = fwm.cxml.UsePort;
}


When FormSetIPAndPort closes I am doing this:


//clicks the OK button.....
private void buttonOK_Click(object sender, System.EventArgs e)
{
      fwm.cxml.UseIP = this.textBoxIP.Text;
      fwm.cxml.UsePort = this.textBoxPort.Text;
      Close();
}



//Now back in the original calling form......the System.Diagnostics debug output lines will show the changes.....

private void menuItemSetIPandPort_Click(object sender, System.EventArgs e)
{
      FormSetIPAndPort fsipap = new FormSetIPAndPort(this);  //I am passing a reference to FormWITSMain to the constructor for FormSetIPAndPort -- correct????

      fsipap.ShowDialog();
      System.Diagnostics.Debug.WriteLine("IP:  " + cxml.UseIP);     /// new values from dialog box have been assigned
      System.Diagnostics.Debug.WriteLine("Port:  " + cxml.UsePort);  
}
Avatar of BurntSky
BurntSky

Yup, that's correct.  I think you get it now. :)

If you're new to programming (or even just new to C#) I'd recommend that you head down to Barnes & Noble or Borders or whatever bookstore you might have and pick up a book or two.  Even if you think you know enough to get by, you'll be surprised at the little stuff you never quite understood. Trust me.  I've been programming for years and years.  My first few years I thought I understood enough to figure everything out, but once I actually sat down and read a few books I was amazed at how much of the basics I never understood.
>>>>>My first few years I thought I understood enough to figure everything out, but once I actually sat down and read a few books I was amazed at how much of the basics I never understood.

============

How well you understand!!!!  LOL.

I have been programming for 6 years in various languages:  Visual C++, Delphi, MS Access, and now C#.  

You can actually accomplish GREAT THINGS without understanding......I mean REALLY understanding.....how most of it actually works.

I am not saying that it is a good idea to REMAIN this way......just that you can survive just fine not knowing a lot of things.


Now....having said all of that.....I also acknowledge the need to go deeper in knowledge than I have been.
That's exactly right.  Like I said, I went on for a number of years knowing I could get by just fine; but it wasn't until I really dug deep into WHY and HOW things worked they way they did that I really began to truely understand, and I think I'm a much better programmer today because of it.  Reading a few books, taking a couple CS classes... a small price to pay for the expertise you gain from understanding.  And, of course, with expertise comes better pay ;)
BurntSky:

Well said!!!

It is that "deeper knowledge" that I now seek to obtain.