CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2011
    Posts
    5

    Question Multiple Forms W/ Passing Value Problem

    I'm currently working on a project on GUI with math model. I'm a Math student and my programming knowledge is limited. I need some helps on my program design.

    //In the Form1, I have

    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    public double contactR;

    private void button1_Click(object sender, EventArgs e)
    {
    if (rdButton1.Checked)
    {
    contactR = 0.1;
    }
    else if (rdButton2.Checked)
    {
    contactR = 0.2;
    }
    else if (rdButton3.Checked)
    {
    contactR = 0.3;
    }
    else if (rdButton4.Checked)
    {
    contactR = 0.4;
    }

    Form2 secondForm = new Form2(contactR.ToString());
    this.Hide();
    secondForm.Show();
    }
    }

    // I pass a string Form 2 and here is the source code
    {
    public Form2(string contactR)
    {
    InitializeComponent();

    }

    public double infectiveR;

    private void button1_Click(object sender, EventArgs e)
    {
    if (rdButton1.Checked)
    {
    infectiveR = 0.33;
    }
    else if (rdButton2.Checked)
    {
    infectiveR = 0.25;
    }
    else if (rdButton3.Checked)
    {
    infectiveR = 0.20;
    }
    else if (rdButton4.Checked)
    {
    infectiveR = 0.17;
    }
    else if (rdButton5.Checked)
    {
    infectiveR = 0.14;
    }
    else if (rdButton6.Checked)
    {
    infectiveR = 0.10;
    }

    Form3 thirdForm = new Form3();
    this.Hide();
    thirdForm.Show();
    }
    }
    -----------------------------------------------------------------------------------------------------
    If I add a MessageBox.Show(contactR); in
    public Form2(string contactR)
    {
    InitializeComponent();

    }
    It works well and it shows that value that I choose in Form1.
    However I would like to know is there any method to pass the string contactR from "public Form2(string contactR)" to "private void button1_Click(object sender, EventArgs e)"?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Multiple Forms W/ Passing Value Problem

    Create a separate class (call it MathModel?) that contains the data that you want to pass around to the various forms.

    Create an instance of that class and modify the constructors of the forms to pass the instance around.

    Another approach is to Make the MathModel class a singleton. In fact, let's do that approach.
    Code:
    class MathModel
    {
      private static MathModel _instance;
      private static Object _instanceLock = new Object();
    
      public static MathModel Instance
      {
        get
        {
          lock( _instanceLock )
          {
            if( _instance == null ) _instance = new MathModel( );
    
            return _instance;
          }
        }
      }
    
      public double ContactR { get; set; }
      public double InfectiveR { get; set; }
    }
    Next, to use this....
    Code:
    public partial class Form1 : Form
    {
      public Form1()
      {
        InitializeComponent();
      }
    
      private void button1_Click(object sender, EventArgs e)
      {
        if (rdButton1.Checked)
        {
          MathModel.Instance.ContactR = 0.1;
        }
        else if (rdButton2.Checked)
        {
          MathModel.Instance.ContactR = 0.2;
        }
        else if (rdButton3.Checked)
        {
          MathModel.Instance.ContactR = 0.3;
        }
        else if (rdButton4.Checked)
        {
          MathModel.Instance.ContactR = 0.4;
        }
    
        Form2 secondForm = new Form2( );
        this.Hide();
        secondForm.Show();
      }
    }
    
    public partial class Form1 : Form
    {
      public Form2()
      {
        InitializeComponent();
      }
    
      private void button1_Click(object sender, EventArgs e)
      {
        if (rdButton1.Checked)
        {
          MathModel.Instance.InfectiveR = 0.33;
        }
        else if (rdButton2.Checked)
        {
          MathModel.Instance.InfectiveR = 0.25;
        }
        else if (rdButton3.Checked)
        {
          MathModel.Instance.InfectiveR = 0.20;
        }
        else if (rdButton4.Checked)
        {
          MathModel.Instance.InfectiveR = 0.17;
        }
        else if (rdButton5.Checked)
        {
          MathModel.Instance.InfectiveR = 0.14;
        }
        else if (rdButton6.Checked)
        {
          MathModel.Instance.InfectiveR = 0.10;
        }
    
        Form3 thirdForm = new Form3();
        this.Hide();
        thirdForm.Show();
      }
    }
    Btw, whenever possible retain the data type as you pass the object around. Previously you declared ContactR as a double, but you changed it's type to a string when you passed it to Form2. In general, it's better to pass it as the original type (i.e. double), and then if you need it as some other type, convert it where you need it.

    Of course, since a class instance (i.e. MathModel) is used to retain the data and the class is a singleton, there is no need to pass data around in a constructor anymore. So whether you pass in ContactR as a string or not doesn't matter (since we aren't passing in ContactR).

  3. #3
    Join Date
    Jul 2011
    Posts
    5

    Re: Multiple Forms W/ Passing Value Problem

    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured