bobmack37 Posted April 23, 2003 Posted April 23, 2003 (edited) erorr saying Cast from string "67 85 90 " to type 'Double' is not valid. I don't even 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 don't even have Double typed what am I doing wrong? Edited April 23, 2003 by Robby Quote
*Experts* jfackler Posted April 23, 2003 *Experts* Posted April 23, 2003 vAvg = (vExam1 + vExam2 + vExam3 + vfinal * 2) / 5 You're adding three strings together and adding them to a string multiplied times 2 and dividing some answer (what that would be I'm 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 doesn't know how to do that so it's trying to cast it to double so it can divide it by 5. If you want to do the division, you'll 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 Quote
Moderators Robby Posted April 23, 2003 Moderators Posted April 23, 2003 Try this... '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)) Quote Visit...Bassic Software
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.