• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

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?
0
Tom Knowlton
Asked:
Tom Knowlton
  • 3
  • 3
1 Solution
 
BurntSkyCommented:
Actually, that's a great way to do it.  Your fear of passing around "the entire FormWITSMain object" is completely unfounded.  One thing you'll have to learn very quickly if you hope to do any professional programming is the idea of references.  You're not actually passing around the entire object, you're just passing around a little number that shows where that object resides in memory.  So passing the reference to the first Form in the second's constructor doesn't create a copy of it; it just lets the second form know where in memory it can access it.

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;
0
 
Tom KnowltonWeb developerAuthor Commented:
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);  
}
0
 
BurntSkyCommented:
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.
0
Industry Leaders: 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!

 
Tom KnowltonWeb developerAuthor Commented:
>>>>>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.
0
 
BurntSkyCommented:
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 ;)
0
 
Tom KnowltonWeb developerAuthor Commented:
BurntSky:

Well said!!!

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now