Jump to content
Xtreme .Net Talk

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


Recommended Posts

Posted (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 by Robby
  • *Experts*
Posted

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

  • Moderators
Posted

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))

Visit...Bassic Software

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...