0

I am a beginner in visual basic, I am developing a calculator in which i have the following piece of code.

labeltotal.text = (radiobutton1.Checked) * 1000

My expectation from the above code is that , if the radiobutton1 is checked , the value of the total should be 1000 otherwise it should be 0.

But what i am getting is , if radiobutton1 is checked, total value changes to -1000 otherwise it goes to 0.

But if i use checkbox , i get correct values. For example, labeltotal.text = (checkbox1.Checkstate) * 1000 it givse me correct values as expected depending on the checkstate.

How can i make the radio button to behave the same way as checkbox in above code?

S_Ananth
  • 107
  • 4
  • 9

1 Answers1

2

First of all, you should switch Option Strict to On.

If a Boolean is converted to Integer, you'll get -1 for True and 0 for False.
For further information see: Convert Boolean to Integer in VB.NET

So one of the following should return the result you want:

labeltotal.Text = (CInt(radiobutton1.Checked) * 1000 * -1).ToString

labeltotal.Text = (CInt(radioButton1.Checked) ^ 2 * 1000).ToString

There's also the function Convert.ToInt32 which will return a 1 for True:

labeltotal.Text = (Convert.ToInt32(radioButton1.Checked) * 1000).ToString

But in this specific case, calculation does not really make sense.
The following is more clear and fits better for this case:

labeltotal.Text = If(radioButton1.Checked, "1000", "0")
MatSnow
  • 7,357
  • 3
  • 19
  • 31