0

I've got a simple .net app with one main window. There's some radio buttons on there that I want to check before processing the data. I have two sets of CS files. Form1.cs (The main window code) and the database.CS (The code that actually runs the DB queries.) database.cs needs to be able to read some settings from the Form1.cs.

On form1.cs I have this:

public string GetWorld
{
    get
    {

        if (this.radioButton_Dev.Checked == true)
        {
            MessageBox.Show("Returning Dev!");
            return "Dev";
        }
        else if (this.radioButton_Prod.Checked == true)
        {
            MessageBox.Show("Returning Prod!");
            return "Prod";
        }
        else
        {
            MessageBox.Show("Returning default!");
            return "Dev";
        }
    }
}

And in database.cs I have this:

  public SqlConnection GetConnectionString () {
        Form1 MainWindow;
        MainWindow = new Form1();
        if (MainWindow.GetWorld == "Dev" )
         {             
            SqlConnection Connection = new SqlConnection("Data Source = Dev .... blah blah blah...");
            return Connection;
        }
        else if (MainWindow.GetWorld == "Prod")
        {
            SqlConnection Connection = new SqlConnection("Data Source = Prod .... blah blah blah...");
            return Connection;
        }           
        else
        {
            SqlConnection Connection = new SqlConnection("Data Source = Dev .... blah blah blah...");
            return Connection;
        }   
    }

The problem I have is that no matter what radio Button I have checked, it always selects the top dev option.

There are some workarounds I could put in place (Make the Radio Button get passed as part of the call to the database.CS) but I don't understand why this isn't working right. To me, it seems like the Method GetConnectionString() is basically pulling the form data when the app launches and never actually looking it up.

If Run this bit of code inside of Form1:

    private void button1_Click(object sender, EventArgs e)
    {
        string blah = GetWorld;
        MessageBox.Show(blah);
    }

It updates correctly.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
EtanSivad
  • 487
  • 8
  • 16
  • I would actually use different config files for different environments. – Daniel A. White Mar 15 '13 at 17:07
  • 2
    Form1 MainWindow = new Form1()...and then you check for the value. Where the user can select it?! (= didn't you forget to show the window to wait for user input?) – Adriano Repetti Mar 15 '13 at 17:08
  • 1
    Passing the selected option seems like a much better idea than creating and using a new form (where the dev option appears to be selected by default) in the database code. – Sam Mar 15 '13 at 17:09

2 Answers2

5

You're creating new Form every time you call GetConnectionString method, it doesn't take your choice, instead it takes default (Dev).

There are couple of solutions to this problem, you can pass your choice as a parameter:

GetConnectionString(string world) {}
// or
GetConnectionString(MyEnum world) {}

and then in your Form:

string str = GetConnectionString(this.GetWorld);
Zbigniew
  • 27,184
  • 6
  • 59
  • 66
3

In this line:

MainWindow = new Form1();

you are creating a completely new Form1. This is not a reference to the window that you are displaying in your program, and its radio button will be in the initial position, hence why it is always returning the same value.

A few options you have are:

  • Pass a reference to your main window into the GetConnectionString() method.
  • Pass the MainWindow.GetWorld value into the GetConnectionString() method (preferable).
JLRishe
  • 99,490
  • 19
  • 131
  • 169