I'm a student just starting my first C# class in school, our first assignment was to make a simple console app to convert temperatures from Fahrenheit to Celsius. Being the over-achiever that I am, I decided to make a second project and create a form app. It's a very simple project that I have almost complete and I have one problem with it that is driving me nuts. I used 2 radio buttons for the user to determine whether to convert to Fahrenheit or to Celsius, they select a button, enter a temperature into a text box and click the convert button(button1) here is the code for button1:

``````public void button1_Click(object sender, EventArgs e)
{
bool convertToCelsius_CheckChanged = true;
{
convToF(double.Parse(Current.Text));
}
else if (convertToCelsius_CheckedChanged ==true)
{
convToC(double.Parse(Current.Text));
}

}
void convToF(double tempC)
{
double convertToFahr1 = tempC * 9 / 5;
double convertToFahr2 = convertToFahr1 + 32;
MessageBox.Show("F=(C*9/5)+32= " + convertToFahr2);
}
void convToC(double tempF)
{
double convertToCels1 = tempF - 32;
double convertToCels2 = convertToCels1 * 5 / 9;
MessageBox.Show("C=(f-32)*5/9= " + convertToCels2);
}``````

The problem is, whenever the form is run, and the convert button is pressed, it only displays the Fahrenheit conversion no matter what radio button is selected. I've searched numerous tutorials and my text book, I've tried every possible combination of true and false bool statements, ifs, elses, else ifs...and still will only display the fahrenheit value...can anyone point me in the right direction?

Check out this part of your code:

``````...
bool convertToCelsius_CheckChanged = true;

{
convToF(double.Parse(Current.Text));
...``````

Won't this always be the case...? Should the first 2 statements be moved elsewhere?

All 4 Replies

Check out this part of your code:

``````...
bool convertToCelsius_CheckChanged = true;

{
convToF(double.Parse(Current.Text));
...``````

Won't this always be the case...? Should the first 2 statements be moved elsewhere?

Check out this part of your code:

``````...
bool convertToCelsius_CheckChanged = true;

{
convToF(double.Parse(Current.Text));
...``````

Won't this always be the case...? Should the first 2 statements be moved elsewhere?

hmm...i didnt think it would be true, because you can only check one radio box at a time...i've moved the bools a couple different places...i'll play around with it n see what i come up with. Thanks for the input, much appriciated!

He was referring more to the fact that you set radioButton1_CheckedChanged = true
and then two lines later have an if statement checking if it is true or false. It can't possibly be false so the second part of your if statement never fires.

He was referring more to the fact that you set radioButton1_CheckedChanged = true
and then two lines later have an if statement checking if it is true or false. It can't possibly be false so the second part of your if statement never fires.

Thanks so much to both of you! I did get the program working properly. However I'm not sure why what I did worked. I understand part of it, I moved the bool statements about the radio button classes, and then in the classes i changed the bool values accordingly. But this didn't work at first, what finally worked was in each statement where it said '_CheckedChanged', I deleted the Changed from the ends. But I didn't delete it from the class names. Since I'm literally teaching myself this as I'm going along, I'm kind of confused as to what happened there and why it worked? And again, thank you both for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.