"Conversion from string " " to type 'Double' is not valid"
As Idle_Mind said in their answer, Double.TryParse is the way to go. This provides a safe way to attempt to convert a value to a double. If the conversion succeeds, the method returns true, and the resulting double is returned in the out parameter. If the conversion fails, false is returned and the default value of double (which is 0) is returned.
A simple example:
Dim result As Double
Dim score As String = "75"
If Double.TryParse(score, result) Then
' result will be a double with the value of 75
Else
' The conversion attempt failed, and result will have a value of 0
End If
So to apply that to your method (with no validation, though Idle_Mind's answer gives a good approach):
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles confirm.Click
Dim sum As Double
Dim modulemark As Double
Dim testScore As Double
Dim projectScore As Double
Dim quizScore As Double
Dim marks As Double
Dim examScore As Double
Double.TryParse(test.Text, testScore)
Double.TryParse(project.Text, projectScore)
Double.TryParse(quiz.Text, quizScore)
Double.TryParse(CAmarks.Text, marks)
Double.TryParse(exam.Text, examScore)
sum = (testScore * .5) + (projectScore * .3) + (quizScore * .3)
modulemark = (marks * .5) + (examScore * .5)
Dim Grade As String
If sum < 40 Then
Grade = "F"
ElseIf sum >= 40 And modulemark < 65 And modulemark >= 40 Then
Grade = "C"
ElseIf sum >= 40 And modulemark < 75 And modulemark >= 65 Then
Grade = "B"
Else
Grade = "A"
End If
End Sub
Explanation of the above code.
First, 6 Double
variables are declared - this necessary because Double.TryParse
takes an out
parameter as the second argument, and that must be declared before its used. You could use one variable (and reuse it), but for simplicity I chose one for each score.
Once the scores have been parsed (successfully or not) the cumulative, weighted totals are determined. Note that parentheses were used when applying the weight modifier, to ensure operator precedence doesn't give you a result other than expected.
Hopefully this clarifies things for you.
![gheys damn](https://lh4.googleusercontent.com/-PnNAIM-hA9Q/AAAAAAAAAAI/AAAAAAAAABk/xdywyOjPfss/photo.jpg?sz=256)
gheys damn
Updated on March 18, 2020Comments
-
gheys damn over 4 years
I am doing a project in microsoft visual studio 2012 and i am trying to write an application to determine the module average.
The script is the following:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles confirm.Click Dim sum As Double sum = CDbl(test.Text) * 50% + CDbl(project.Text) * 30% + CDbl(quiz.Text) * 30% Dim modulemark As Double modulemark = CDbl(CAmarks.Text) * 50% + CDbl(exam.Text) * 50% Dim Grade As String If sum < 40 Then Grade = "F" ElseIf sum >= 40 And modulemark < 65 And modulemark >= 40 Then Grade = "C" ElseIf sum >= 40 And modulemark < 75 And modulemark >= 65 Then Grade = "B" Else Grade = "A" End If
The script is intended to calculate the marks and give a grade after clicking a button named "Confirm".
However, when i tried to run the coding it said:
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from string "" to type 'Double' is not valid.
can someone see what is wrong? i am new to Visual studio and i appreciate your help.
P.S. edited recent script.
P.S. Thank you for the user "Tim" for the script
but for some unknown reason on the line "Double.TryParse(caMarks.Text, caMarks)" there's a blue squiggly that directs to caMarks that says "'Text' is not a member of 'double'". This is literally pulling my hair off! please help!It looks like the names of the objects conflicted.
-
Idle_Mind over 9 yearsYou need to move from using all of those CDbl() calls to proper Double.TryParse() calls. Look it up...
-
Idle_Mind over 9 yearsAdditonally, it is unclear if your use of "50%" is correct. You most likely meant that to be ".5". The "%" symbol causes that 50 to be treated as an integer, which it already is. See Type Characters (Visual Basic).
-
gheys damn over 9 yearsTried. It said: "Overload resolution failed because no accessible tryparse accepts this number of arguments." The script is: "modulemark = Double.TryParse(CAmarks.Text) * 50% + Double.TryParse(exam.Text) * 50%". the dim is already set as double.
-
gheys damn over 9 yearsNow, on the line "Double.TryParse(CAMarks.Text, CAMarks)" has an error that says:"'Text' is not a member of 'Double'", and only on that line and on CAmarks without any apparent reason. without that there are no problems. once i tried getting that line in, it appears.
-
Tim over 9 yearsForgot VB.NET isn't case-sensitive. Change the double
caMarks
tomarks
and try again. The reason you got the error was because VB.NET sawCAMarks
(The textbox) andcaMarks
(the double) as the same name.