Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm making a Student Information System where a student can enter there results and calculate their final grade overall. In the group box "Project Results" students enter there 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"


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. Thank you !


VB
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


What I have tried:

I've tried researching the exception type "System.InvalidCastException' and also how to convert String to type Boolean however was unsuccessful in gaining enough insight to fix the problem
Posted
Updated 23-Mar-16 3:41am
v2

Try:
VB.NET
If IsNumeric(TextBox3.Text) AndAlso CheckBox2.Checked Then

The Checked property returns a Boolean. You were converting that to a string, and then attempting to use it as a Boolean. By removing the .ToString, you leave it as a Boolean, which can be used in a conditional expression.

Also, the operator in VB is AndAlso, not &.
 
Share this answer
 
in Addition to Richards Solution

the & in VB is for string concatenation
in VB the AND Operator ist simply And and short-circuit AndAlso
simmilar Or and OrElse

Also IsNumeric is old and normal you want to use TYPE.TryParse of the specific type you want.

and are you sure about Decimal[^]? its a 128bit field.. that maths don't really look like you need that.. maybe just go with Double[^]

and make yourself a favour, give that controls better names..
like CheckBox2 -> cb_7DaysLate
gives a better hint when you look over that function in a week.
similar to that TextBoxes

VB
Dim input As Double
If Double.TryParse(TextBox3.Text, input) AndAlso cb_7DaysLate.Checked AndAlso input <= 20 Then
     Dim _temp As Double = input * 5
     TextBox4.Text = _temp.ToString
     TextBox5.Text = (_temp * 0.1 - 0.1).ToString
Else
     MessageBox.Show("Please Enter valid value equal to 20 and below!")
End If
 
Share this answer
 
Using following methods in C#

bool val;
string input;

input = bool.TrueString;
val = bool.Parse(input);
Console.WriteLine("'{0}' parsed as {1}", input, val);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900