erorr saying Cast from string "67 85 90 " to type 'Double' is not valid. I don't even

bobmack37

Member
Joined
Apr 22, 2003
Messages
12
Code:
Dim vfstudentG As String
Dim vsocSec As String
Dim vExam1, vExam2, vExam3, vfinal, vAvg, vtotal As String
Dim fmtstr As String = "{0,-15}{1,8:n}"
Private Sub btncompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncompute.Click
lstout.Items.Clear()
lstout.Items.Add(String.Format(fmtstr, "Soc.Sec.No.", _
"Average" ))
FileOpen(1, "student grades.txt", OpenMode.Input)

Input(1, vfstudentG)
vsocSec = vfstudentG.Substring(0, 11)
vExam1 = vfstudentG.Substring(12, 3)
vExam2 = vfstudentG.Substring(15, 3)
vExam3 = vfstudentG.Substring(18, 3)
vfinal = vfstudentG.Substring(21)
vAvg = (vExam1 + vExam2 + vExam3 + vfinal * 2) / 5
lstout.Items.Add(String.Format(fmtstr, vsocSec, vAvg))
vtotal = vAvg

End Sub
End Class
This is just the code I have typed so far.
Ok the problem I am having is the line Vavg = (vExam1 + vExam2 + vExam3 + vfinal * 2) / 5 when I get to this line the program pukes on me giving me an erorr saying Cast from string "67 85 90 " to type Double is not valid. I dont even have Double typed what am I doing wrong?
 
Last edited by a moderator:
vAvg = (vExam1 + vExam2 + vExam3 + vfinal * 2) / 5

Youre adding three strings together and adding them to a string multiplied times 2 and dividing some answer (what that would be
Im not sure) by 5.

The problem is "67 85 90" is a string (not sure how it got that, but I guess it answers my previous puzzlement.). Your trying to divide it by 5.

Your code doesnt know how to do that so its trying to cast it to double so it can divide it by 5.

If you want to do the division, youll need to convert your vExams to something that can be divided by 5.

That answer will then need to be converted to string if you want to store it in vAvg (because you dimensioned it as a string).

Make sure you have turned on option strict and option explicit.

It will save you from a good portion of these errors.

Jon
 
Try this...
Code:
change this...
Dim vExam1, vExam2, vExam3, vfinal, vAvg, vtotal As String

to this...
Dim vExam1, vExam2, vExam3, vfinal, vAvg, vtotal As Double

then do the following
vExam1 = ctype(vfstudentG.Substring(12, 3),double)
vExam2 = ctype(vfstudentG.Substring(15, 3),double)
vExam3 = ctype(vfstudentG.Substring(18, 3),double)
vfinal = ctype(vfstudentG.Substring(21),double)

vAvg = (vExam1 + vExam2 + vExam3 + vfinal * 2) / 5
lstout.Items.Add(String.Format(fmtstr, vsocSec, vAvg.tostring))
 
Back
Top