0

I'm making a Student Information System where a student can enter their results and calculate their final grade overall. In the group box "Project Results" students enter their project results out of 50 and their percentage is calculated in TextBox1.

However, I want to include checkboxes to suit certain conditions; e.g If checkbox2 is checked this means the project was submitted 7 days late = 10 % deduction in the final grade. I've completed the following code but am having a run time error of:

Conversion from string "TrueTrue" to type 'Boolean' is not valid

enter image description here

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim a As Integer
    Dim b As Decimal

    If IsNumeric(TextBox3.Text) & CheckBox2.Checked.ToString Then
        If TextBox3.Text <= 20 Then
            a = (TextBox3.Text * 100) / 20
            TextBox4.Text = a
            b = a * 0.1 - 0.1

            TextBox5.Text = CDec(b)




        Else
            MsgBox("Please Enter value equal to 20 and below!")
        End If
    End If

I'm wondering how I can convert a Boolean value to a String, or even if there is a better of way of completing my task?

James Thorpe
  • 31,411
  • 5
  • 72
  • 93
MLL
  • 43
  • 1
  • 9
  • 2
    If you switch `Option Strict On` you will see your error straight away as this won't compile. That should give you a pointer – Matt Wilko Mar 22 '16 at 13:47
  • Hi, If somebody's answer works for you, click on the tick next to it. - It helps other users who are searching for solutions. They can see that an answer to a question has been accepted and may work for them. And of course the person that answered the question gets reputation - And (thanks to @JamesThorpe) you get 2 rep for accepting as well. – David Wilson Mar 23 '16 at 11:00

2 Answers2

4

CheckBox2.Checked is already a boolean. When you add ToString on the end, you're forcing it to be a string. This, coupled with the & before it, which is the string concatenation operator, is why you're ending up with "TrueTrue". What you want is this:

If IsNumeric(TextBox3.Text) And CheckBox2.Checked Then

Side note: the checkboxes you have there imply that the user should only be choosing one of them (something can't simultaneously be On Time and late, for instance) - Radio Buttons would be a better UI choice here.

James Thorpe
  • 31,411
  • 5
  • 72
  • 93
  • Works perfectly thank you, I've also changed Checkboxes to Radio Buttons instead! – MLL Mar 22 '16 at 14:16
0

You should change the line to

If IsNumeric(TextBox3.Text) And CheckBox2.Checked Then

or check faster with

If IsNumeric(TextBox3.Text) AndAlso CheckBox2.Checked Then

Why?

VB uses And instead of & and uses AndAlso instead of &&.

The & Operator in VB is used for forcing string concatenation.

So the line If IsNumeric(TextBox3.Text) & CheckBox2.Checked.ToString Then (if both is true)

Equals If True & True Then

And become If "TrueTrue" Then

Since there are no auto conversion from type String to Boolean, so the exception is thrown.

Using AndAlso will be a little faster than using And, because it doesn't evaluate subsequent conditions if an earlier condition proves false.

Comparison between VB and C++

VB      | C++
And     | &
AndAlso | &&

Note

@James Thorpe's solution works too, but I suggest using AndAlso.

See also:

Which is better for performance? And vs AndAlso

Differences in boolean operators: & vs && and | vs ||

Community
  • 1
  • 1
J3soon
  • 3,013
  • 2
  • 29
  • 49
  • While you're correct about the performance, in this situation where something is only going to run once in one calculation, and is done after an `IsNumeric` and is only checking a boolean property rather than doing anything complex, I suspect that the performance difference is never going to be worth worrying about here. – James Thorpe Mar 22 '16 at 13:48
  • 1
    @JamesThorpe Yes, but I think it's something that programmers should be aware of. See [this](http://stackoverflow.com/questions/4014535/differences-in-boolean-operators-vs-and-vs) article. – J3soon Mar 22 '16 at 13:54
  • 1
    Absolutely, I'm just saying that it's not worth worrying about _here_. It's a good addendum to your answer, but what would improve your answer here would be to explain _why_ the OP should change that line of code. You've gone straight from "you should do this" without a "why you should do this" to something related but doesn't explain the actual problem at all. – James Thorpe Mar 22 '16 at 13:56