Results 1 to 4 of 4

Thread: Help Please [Practice for an assessment]

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    1

    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

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,385

    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
       '....
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    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.

  4. #4
    Addicted Member
    Join Date
    Apr 2010
    Posts
    131

    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:
    1. Public Class Form1
    2.     Dim FrenchScore, GermanScore, SpanishScore As Single
    3.     Dim French, German, Spanish As String
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         'Note Pass mark for all 3 is a score of 25
    7.  
    8.         'French Scores
    9.         FrenchScore = CSng(InputBox("Please Enter Pupils French Score"))
    10.         Do Until FrenchScore >= 1 And FrenchScore <= 40 And FrenchScore Mod 1 = 0
    11.             FrenchScore = CSng(InputBox("Error Pupils grades cannot be a half mark or cannot be below 1 or over 40. Please enter Pupils Grade Again"))
    12.         Loop
    13.  
    14.         'German Scores
    15.  
    16.         GermanScore = CSng(InputBox("Please Enter Pupils German Score"))
    17.         Do Until FrenchScore >= 1 And GermanScore <= 40 And GermanScore Mod 1 = 0
    18.             GermanScore = CSng(InputBox("Error Pupils grades cannot be a half mark or cannot be below 1 or over 40. Please enter Pupils Grade Again"))
    19.         Loop
    20.  
    21.         'Spanish Scores
    22.  
    23.         SpanishScore = CSng(InputBox("Please Enter Pupils Spanish Score"))
    24.         Do Until SpanishScore >= 1 And GermanScore <= 40 And SpanishScore Mod 1 = 0
    25.             SpanishScore = CSng(InputBox("Error Pupils grades cannot be a half mark or cannot be below 1 or over 40. Please enter Pupils Grade Again"))
    26.         Loop
    27.  
    28.         'Checking If Passed or failed
    29.  
    30.         If FrenchScore <= 40 Then French = "Passed" Else French = "Failed"
    31.         If GermanScore <= 40 Then German = "Passed" Else German = "Failed"
    32.         If SpanishScore <= 40 Then Spanish = "Passed" Else Spanish = "Failed"
    33.  
    34.         'Displaying
    35.  
    36.         MsgBox("The Pupil Has " & French, " French, " & German, " German and ")
    37.     End Sub
    38.  
    39.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    40.  
    41.     End Sub
    42. 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
  •  



Click Here to Expand Forum to Full Width