Implicit Conversion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • candy1
    New Member
    • Feb 2009
    • 2

    Implicit Conversion

    Hello,
    I am new to VB I get this error message "Conversion from string "" to type 'Double' is not valid. when running my app, (the underline codes are the errors). which is a basketball simulator. How can I correct this; Thank you so much for taking the time to review this.
    Code:
    Private Sub playButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles playButton.Click
            Dim rank As Integer
            Dim player As Integer
            Dim shoots As Integer
            Dim RandomGenerator As New Random
            Dim isConverted As Boolean
            Dim I As Integer = 0
            'generate random player from visitor 6-10, home 1-5
            player = RandomGenerator.Next(1, 11)
            Label84.Text = CStr(RandomGenerator.Next(1, 101))
    
            For I = 0 To 4
                Select Case player
                    Case 1
                        Label1.Text = "Home Player 1 Selected"
                        If TextBox6.Text < TextBox90.Text And Label84.Text < TextBox66.Text Then
                            TextBox30.Text = +1[U] And CDbl(TextBox25.Text) = +1 [/U]And CDbl(TextBox20.Text) = +2
    
                        Else
                            TextBox45.Text = +1[U] And CDbl(TextBox39.Text) = +1[/U] And CDbl(TextBox35.Text) = +2
    
                        End If
    
                    Case 2
                        Label1.Text = "Home Player 2 Selected"
                        If TextBox7.Text < TextBox9.Text And Label84.Text < TextBox12.Text Then
                            TextBox29.Text = +1 [U]And CDbl(TextBox24.Text) = +1 [/U]And CDbl(TextBox19.Text) = +2
                        Else
                            TextBox44.Text = +1 [U]And CDbl(TextBox39.Text) = +1[/U] And CDbl(TextBox34.Text) = +2
                        End If
                    Case 3
                        Label1.Text = "Home Player 3 Selected"
                        If TextBox8.Text < TextBox88.Text And Label84.Text < TextBox78.Text Then
                            TextBox28.Text = +1 [U]And CDbl(TextBox23.Text) = +1 [/U]And CDbl(TextBox18.Text) = +2
                        Else
                            TextBox43.Text = +1 [U]And CDbl(TextBox38.Text) = +1[/U] And CDbl(TextBox33.Text) = +2
                        End If
                    Case 4
                        Label1.Text = "Home Player 4 Selected"
                        If TextBox9.Text < TextBox81.Text And Label84.Text < TextBox77.Text Then
                            TextBox27.Text = +1 [U]And CDbl(TextBox22.Text) = +1[/U] And CDbl(TextBox17.Text) = +2
                        Else
                            TextBox42.Text = +1 [U]And CDbl(TextBox37.Text) = +1[/U] And CDbl(TextBox32.Text) = +2
                        End If
                    Case 5
                        Label1.Text = "Home Player 5 Selected"
                        If TextBox10.Text < TextBox86.Text And Label84.Text < TextBox76.Text Then
                            TextBox26.Text = +1 [U]And CDbl(TextBox22.Text) = +1 [/U]And CDbl(TextBox17.Text) = +2
                        Else
                            TextBox41.Text = +1[U] And CDbl(TextBox36.Text) = +[/U]1 And CDbl(TextBox31.Text) = +2
                        End If
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    What on earth is this line doing:
    TextBox30.Text = +1 And CDbl(TextBox25. Text) = +1 And CDbl(TextBox20. Text) = +2
    That doesn't even look like a logical assignment of anything

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      I think that the following line

      Code:
      TextBox30.Text = +1 And CDbl(TextBox25.Text) = +1 And CDbl(TextBox20.Text) = +2
      Is actually supposed to be:
      Code:
      TextBox30.Text = "+1" And CDbl(TextBox25.Text) = 1 And CDbl(TextBox20.Text) = 2
      Or maybe (probably more likely) it should be:
      Code:
      TextBox30.Text = "+1" And TextBox25.Text= "+1"  And TextBox20.Text = "+2"
      When you are comparing Strings make sure that the values being compared are Strings.

      For example:
      TextBox30.Text will return a String.
      In order to check if this string contains "+1" you have to compare it to a String that contains "+1".

      The following will check to see if the TextBox contains "+1" as it's value:

      Code:
      If TextBox30.Text = "+1" Then
      Please note the double quotes around the +1 value. This is required for the comparison to work.

      Now, say you're expecting the TextBox to contain a Number, and you want to see if that number is equal to 1 (a positive 1, not a negative 1).

      First of all, you cannot compare numbers to Strings. They are different animals. So you have to either cast or parse the String into a number, or you have to convert the number into a String.

      To cast a String into a Number (a type Double holds a number) you would do something like:
      Code:
      Dim myNumber As Double = CDbl(TextBox30.Text)
      Or
      Code:
      Dim myNumber As Double =CType(TextBox30.Text,Double)
      Or
      Code:
      Dim myNumber As Double =DirectCast(TextBox30.Text,Double)
      Please note that if the String does not contain a Number, then casting will throw an Exception. A String containing the value of "+1" is not a number. Therefore the cast is failing in your case....

      If you want to convert a String into a number, you can use the Parse() method.

      For example:
      Code:
      Dim myNumber As Double =Double.Parse(TextBox30.Text)
      The above code will attempt to convert the text in TextBox30 into a Double.
      If the text is not a number (don't forget that "+1" is not a number), this will also thrown an exception.

      Since (I'm assuming) you are checking if the TextBox contains "+1", you cannot convert this value into a number. This is a String and needs to be compared to another String. Therefore, I recommend you change your code and compare Strings instead of casting the text into a number:
      Code:
      If TextBox30.Text = "+1" And TextBox25.Text= "+1"  And TextBox20.Text = "+2" Then
      I find it incredible that the compiler even got so far to throw the exception that you're getting!

      You Must realize that a "+" is an Operator that preforms addition on 2 Objects.

      For example adding 2 numbers:
      1 + 1

      Will result in
      2

      Adding two Strings:
      "1" + "1"

      Will result in the two Strings being put together:
      "11"

      When you start mixing numbers and Strings things get messed up.
      The compiler get's confused about what you really want.
      For example say you attempt to add a String and a number together:
      "1" + 1

      What do you want?
      Do you want the number to be converted into a String?
      Or do you want the string to be converted into a number?

      This is why the compiler is not happy with what you've done.
      You're taking the String stored in your TextBox and attempting to check if it's equal to "something" added it to a number:
      Code:
      TextBox30.Text = +1
      The compiler's throwing an error because it doesn't know what you are doing.
      (We actually don't know what you're doing either)

      The lesson here is: don't mix types.
      Tell the computer exactly what you want and it will do what you ask.

      Comment

      • candy1
        New Member
        • Feb 2009
        • 2

        #4
        Thank you

        Thank you for taking the time to go through my code. What I want the computer to due it the conditions are true to add points to the textboxes. Which are scores of the players. If the condition are true then the textboxes store the points for shots attempted, shots made and points made. I am apparently way off. I'm looking for an example of a basketball simulator that has match ups. It is back to the drawing board. Thanks again.

        Comment

        Working...