Help Please [Practice for an assessment]
Hello i was trying to practice for an assessment and i got this error:
HTML Code:
{"Conversion from string "Passed" to type 'Integer' is not valid."}
Here Is My Code
Code:
Public Class Form1
Dim FrenchScore, GermanScore, SpanishScore As Single
Dim French, German, Spanish As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Note Pass mark for all 3 is a score of 25
'French Scores
FrenchScore = Val(InputBox("Please Enter Pupils French Score"))
Do Until FrenchScore >= 1 And FrenchScore <= 40 And FrenchScore Mod 1 = 0
FrenchScore = Val(InputBox("Error Pupils grades cannot be a half mark or cannot be below 1 or over 40. Please enter Pupils Grade Again"))
Loop
'German Scores
GermanScore = Val(InputBox("Please Enter Pupils German Score"))
Do Until FrenchScore >= 1 And GermanScore <= 40 And GermanScore Mod 1 = 0
GermanScore = Val(InputBox("Error Pupils grades cannot be a half mark or cannot be below 1 or over 40. Please enter Pupils Grade Again"))
Loop
'Spanish Scores
SpanishScore = Val(InputBox("Please Enter Pupils Spanish Score"))
Do Until SpanishScore >= 1 And GermanScore <= 40 And SpanishScore Mod 1 = 0
SpanishScore = Val(InputBox("Error Pupils grades cannot be a half mark or cannot be below 1 or over 40. Please enter Pupils Grade Again"))
Loop
'Checking If Passed or failed
If FrenchScore <= 40 Then French = "Passed" Else French = "Failed"
If GermanScore <= 40 Then German = "Passed" Else German = "Failed"
If SpanishScore <= 40 Then Spanish = "Passed" Else Spanish = "Failed"
'Displaying
MsgBox("The Pupil Has " & French, " French, " & German, " German and ")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Re: Help Please [Practice for an assessment]
Val is a terrible way to try and convert strings to integers. Instead, try using Integer.Parse or in your case Integer.TryParse. Also, because you're doing quite a bit of conversions, I suggest turning option strict and option explicit on. To do so, it looks like this:
Code:
Option Strict On
Option Explicit On
Public Class Form1
'....
Re: Help Please [Practice for an assessment]
You defined:
Dim French, German, Spanish As Integer
But then try to set their values to words:
If FrenchScore <= 40 Then French = "Passed" Else French = "Failed"
If GermanScore <= 40 Then German = "Passed" Else German = "Failed"
If SpanishScore <= 40 Then Spanish = "Passed" Else Spanish = "Failed"
"Passed" and "Failed" are Strings, not integers...
Re: Help Please [Practice for an assessment]
Like it was previously mentioned, keep Option strict ON at the top of your code.
Also, I'd suggest you take a look at Data Types in VB.NET
vb.net Code:
Public Class Form1
Dim FrenchScore, GermanScore, SpanishScore As Single
Dim French, German, Spanish As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Note Pass mark for all 3 is a score of 25
'French Scores
FrenchScore = CSng(InputBox("Please Enter Pupils French Score"))
Do Until FrenchScore >= 1 And FrenchScore <= 40 And FrenchScore Mod 1 = 0
FrenchScore = CSng(InputBox("Error Pupils grades cannot be a half mark or cannot be below 1 or over 40. Please enter Pupils Grade Again"))
Loop
'German Scores
GermanScore = CSng(InputBox("Please Enter Pupils German Score"))
Do Until FrenchScore >= 1 And GermanScore <= 40 And GermanScore Mod 1 = 0
GermanScore = CSng(InputBox("Error Pupils grades cannot be a half mark or cannot be below 1 or over 40. Please enter Pupils Grade Again"))
Loop
'Spanish Scores
SpanishScore = CSng(InputBox("Please Enter Pupils Spanish Score"))
Do Until SpanishScore >= 1 And GermanScore <= 40 And SpanishScore Mod 1 = 0
SpanishScore = CSng(InputBox("Error Pupils grades cannot be a half mark or cannot be below 1 or over 40. Please enter Pupils Grade Again"))
Loop
'Checking If Passed or failed
If FrenchScore <= 40 Then French = "Passed" Else French = "Failed"
If GermanScore <= 40 Then German = "Passed" Else German = "Failed"
If SpanishScore <= 40 Then Spanish = "Passed" Else Spanish = "Failed"
'Displaying
MsgBox("The Pupil Has " & French, " French, " & German, " German and ")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class