|
-
Feb 26th, 2013, 03:40 PM
#1
Thread Starter
New Member
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
-
Feb 26th, 2013, 04:02 PM
#2
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
'....
-
Feb 26th, 2013, 04:11 PM
#3
Frenzied Member
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...
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Feb 26th, 2013, 08:45 PM
#4
Addicted Member
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
Last edited by Legjendat; Feb 26th, 2013 at 08:51 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|