none
invalid cast exception is unhandled --visual basic

    Question

  • I am going to start off by saying I am a major newbie. What am I doing wrong with the  section listed below and also with the counter?

                    numGoals = CDbl(InputBox("Enter number of goals for this season"))
                    numAssists = CDbl(InputBox("Enter number of assists for this season"))

    Private Sub txtSeasons_TextChanged(sender As Object, e As EventArgs) Handles txtSeasons.TextChanged

            Dim txtSeasons As String                        ' declaring txtSeasons as string    
            Dim minSeasons As Integer                       ' declaring minimum seasons usable as int
            Dim maxSeasons As Integer                       ' declaring the max # of seasons usable as int
            Dim numGoals As Integer = 0
            Dim numAssists As Integer = 0
            Dim count As Integer = 0
            Dim seasons As Integer = 1

            maxSeasons = 20                                 ' assigning the max seasons usable as 20
            minSeasons = 1                                  ' assigning the min seasons usable as 1

            If IsNumeric(Me.txtSeasons.Text) = True Then
                txtSeasons = Convert.ToDouble(Me.txtSeasons.Text)
            End If

            If (txtSeasons < minSeasons) Or (txtSeasons > maxSeasons) Then
                MessageBox.Show("Please input the number of seasons between 1 and 20!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                btnGetStats.Enabled = False
            End If
            If (txtSeasons >= minSeasons) Or (txtSeasons <= maxSeasons) Then
                Do While count <= txtSeasons
                    count = seasons
                    numGoals = CDbl(InputBox("Enter number of goals for this season"))
                    numAssists = CDbl(InputBox("Enter number of assists for this season"))
                    count += 1

                Loop
                MessageBox.Show("Season" & count & " goals are:" & "" & numGoals & vbNewLine,
                                  "Season" & count & "assists are:" & "" & numAssists)
            End If




        End Sub

    Saturday, November 23, 2013 10:31 PM

Answers

  • The Do loop is resetting the counter, 'count' each time the loop executes. You should move the initializer,

    count = seasons

    outside and before the Do loop.

    The InputBox returns a string so it is possible that it could return a string that fails in CDbl. In addition, if the cancel button is clicked on the InputBox it returns a zero-length string.

    As an alternative, consider using TryParse, like

    Double.TryParse(InputBox("Enter number of goals for this season"), numGoals)
    Double.TryParse(InputBox("Enter number of assists for this season"), numAssists)

    If the string can't be converted to double, the double variable is set to 0 and no error occurs. As such, depending on what you are trying to do, it can be useful to check the Boolean return value from TryParse.

    Good luck!


    - HomeGrownCoder My posts are kept as simple as possible for easier understanding. In many cases you can probably optimize or spruce up what I present. Have fun coding!

    Sunday, November 24, 2013 12:49 AM

All replies

  • Ranger,

    How about re-work your post there and post the code using the code formatter (the button you'll see that has the "<>" shown).

    Since you're starting out, I'll suggest that on this and all of your projects, at the very top of your form, you put the following:

    Option Strict On
    Option Explicit On

    I can't really make out what you're doing in that to know if those settings will see any issues or not, but nevertheless, it's a good practice.

    Lastly though, explain just what this is and what it's supposed to be doing - then explain what's not working right.


    Please call me Frank :)

    Saturday, November 23, 2013 11:13 PM
  • The Do loop is resetting the counter, 'count' each time the loop executes. You should move the initializer,

    count = seasons

    outside and before the Do loop.

    The InputBox returns a string so it is possible that it could return a string that fails in CDbl. In addition, if the cancel button is clicked on the InputBox it returns a zero-length string.

    As an alternative, consider using TryParse, like

    Double.TryParse(InputBox("Enter number of goals for this season"), numGoals)
    Double.TryParse(InputBox("Enter number of assists for this season"), numAssists)

    If the string can't be converted to double, the double variable is set to 0 and no error occurs. As such, depending on what you are trying to do, it can be useful to check the Boolean return value from TryParse.

    Good luck!


    - HomeGrownCoder My posts are kept as simple as possible for easier understanding. In many cases you can probably optimize or spruce up what I present. Have fun coding!

    Sunday, November 24, 2013 12:49 AM