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.