none
Operator '=' is not defined for types 'System.Windows.Forms.TextBox' and 'Integer.'

    Question

  • I'm trying to write a code that changes each letter in your first, middle, and last name to its numerical value, and adds it up. Here's the code

    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        End Sub
        Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
            Dim msg = "Are you sure you want to exit? All progress will be lost."
            Dim title = "Exit"
            Dim style = MsgBoxStyle.YesNo
            Dim response = MsgBox(msg, style, title)
            If response = MsgBoxResult.Yes Then
                Dim msg2 = "Closing program..."
                Dim title2 = "Closing..."
                MsgBox(msg2, , title2)
                End
            Else
            End If
        End Sub
        Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim msg = "Are you sure you want to reset? All progress will be lost."
            Dim title = "Reset"
            Dim style = MsgBoxStyle.YesNo
            Dim response = MsgBox(msg, style, title)
            If response = MsgBoxResult.Yes Then
                Application.Restart()
            Else
            End If
        End Sub
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Text2
            Select Case txtFN
                Case 1
                    Text2.Text = "A"
                Case 2
                    Text2.Text = "B"
                Case 3
                    Text2.Text = "C"
                Case 4
                    Text2.Text = "D"
                Case 5
                    Text2.Text = "E"
                Case 6
                    Text2.Text = "F"
                Case 7
                    Text2.Text = "G"
                Case 8
                    Text2.Text = "H"
                Case 9
                    Text2.Text = "I"
                Case 10
                    Text2.Text = "J"
                Case 11
                    Text2.Text = "K"
                Case 12
                    Text2.Text = "L"
                Case 13
                    Text2.Text = "M"
                Case 14
                    Text2.Text = "N"
                Case 15
                    Text2.Text = "O"
                Case 16
                    Text2.Text = "P"
                Case 17
                    Text2.Text = "Q"
                Case 18
                    Text2.Text = "R"
                Case 19
                    Text2.Text = "S"
                Case 20
                    Text2.Text = "T"
                Case 21
                    Text2.Text = "U"
                Case 22
                    Text2.Text = "V"
                Case 23
                    Text2.Text = "W"
                Case 24
                    Text2.Text = "X"
                Case 25
                    Text2.Text = "Y"
                Case 26
                    Text2.Text = "Z"
            End Select
        End Sub
    End Class

    And I keep getting the error

    Error 1 Operator '=' is not defined for types 'System.Windows.Forms.TextBox' and 'Integer'.

    for every case number.

    Help?

    Edit: I am doing this in Microsoft Visual Studio 2005 in Visual Basic
    • Edited by frankwil96 Monday, September 30, 2013 3:35 PM
    Monday, September 30, 2013 3:27 PM

Answers

  • So you need to Change it to

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim x As New Dictionary(Of Char, Integer)
            Dim Total As Integer
    
    
            For d As Integer = 1 To 26
                x.Add(Chr(d + 64), d)
            Next
    
            For i As Integer = 0 To txtFN.Text.ToCharArray().Length - 1
                Total += x(txtFN.Text.ToCharArray()(i))
            Next
    
            Text2.Text = Total.ToString()
    
        End Sub


    Please remember to 'Mark as Answer' the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    • Edited by Ahmed-Samy Monday, September 30, 2013 4:30 PM
    • Marked as answer by frankwil96 Wednesday, October 02, 2013 3:35 PM
    Monday, September 30, 2013 4:30 PM
  • Change the Select statement 

    Select Case txtFN.Text
                Case "1"
                    Text2.Text = "A"
                Case "2"
                    Text2.Text = "B"
                Case "3"
                    Text2.Text = "C"
                Case "4"
                    Text2.Text = "D"
                Case "5"
                    Text2.Text = "E"
                Case "6"
                    Text2.Text = "F"
                Case "7"
                    Text2.Text = "G"
                Case "8"
                    Text2.Text = "H"
                Case "9"
                    Text2.Text = "I"
                Case "10"
                    Text2.Text = "J"
                Case "11"
                    Text2.Text = "K"
                Case "12"
                    Text2.Text = "L"
                Case "13"
                    Text2.Text = "M"
                Case "14"
                    Text2.Text = "N"
                Case "15"
                    Text2.Text = "O"
                Case "16"
                    Text2.Text = "P"
                Case "17"
                    Text2.Text = "Q"
                Case "18"
                    Text2.Text = "R"
                Case "19"
                    Text2.Text = "S"
                Case "20"
                    Text2.Text = "T"
                Case "21"
                    Text2.Text = "U"
                Case "22"
                    Text2.Text = "V"
                Case "23"
                    Text2.Text = "W"
                Case "24"
                    Text2.Text = "X"
                Case "25"
                    Text2.Text = "Y"
                Case "26"
                    Text2.Text = "Z"
            End Select


    Please remember to 'Mark as Answer' the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by frankwil96 Monday, September 30, 2013 3:48 PM
    Monday, September 30, 2013 3:42 PM
  • I added some code to Ahmed-Samy's post to verify that the textbox input was valid.  Here it is:

        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim x As New Dictionary(Of Char, Integer)
            Dim Total As Integer
    
            Dim mystr As String = TextBox1.Text.ToUpper()
            For Each ch As Char In mystr
                If Not Char.IsLetter(ch) And Not ch = " "c Then
                    MessageBox.Show("Incorrect entry - must be all letters", ch.ToString)
                    TextBox1.Clear()
                    TextBox1.Focus()
                    Exit Sub
                End If
            Next
            mystr = mystr.Replace(" ", "")
    
            For d As Integer = 1 To 26
                x.Add(Chr(d + 64), d)
            Next
    
            For i As Integer = 0 To mystr.ToCharArray().Length - 1
                Total += x(mystr.ToCharArray()(i))
            Next
    
            TextBox2.Text = Total.ToString()
        End Sub

    or, for Armin's post:

        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim x As New Dictionary(Of Char, Integer)
            Dim result As Integer
    
            Dim mystr As String = TextBox1.Text.ToUpper()
            For Each ch As Char In mystr
                If Not Char.IsLetter(ch) And Not ch = " "c Then
                    MessageBox.Show("Incorrect entry - must be all letters", ch.ToString)
                    TextBox1.Clear()
                    TextBox1.Focus()
                    Exit Sub
                End If
            Next
            mystr = mystr.Replace(" ", "")
    
            result = Aggregate c In mystr _
                      Select Asc(c) - 64 _
                      Into Sum()
            TextBox2.Text = result.ToString
    
        End Sub


    • Edited by Solitaire Monday, September 30, 2013 7:00 PM
    • Marked as answer by frankwil96 Wednesday, October 02, 2013 3:34 PM
    Monday, September 30, 2013 6:50 PM

All replies

  • Change the Select statement 

    Select Case txtFN.Text
                Case "1"
                    Text2.Text = "A"
                Case "2"
                    Text2.Text = "B"
                Case "3"
                    Text2.Text = "C"
                Case "4"
                    Text2.Text = "D"
                Case "5"
                    Text2.Text = "E"
                Case "6"
                    Text2.Text = "F"
                Case "7"
                    Text2.Text = "G"
                Case "8"
                    Text2.Text = "H"
                Case "9"
                    Text2.Text = "I"
                Case "10"
                    Text2.Text = "J"
                Case "11"
                    Text2.Text = "K"
                Case "12"
                    Text2.Text = "L"
                Case "13"
                    Text2.Text = "M"
                Case "14"
                    Text2.Text = "N"
                Case "15"
                    Text2.Text = "O"
                Case "16"
                    Text2.Text = "P"
                Case "17"
                    Text2.Text = "Q"
                Case "18"
                    Text2.Text = "R"
                Case "19"
                    Text2.Text = "S"
                Case "20"
                    Text2.Text = "T"
                Case "21"
                    Text2.Text = "U"
                Case "22"
                    Text2.Text = "V"
                Case "23"
                    Text2.Text = "W"
                Case "24"
                    Text2.Text = "X"
                Case "25"
                    Text2.Text = "Y"
                Case "26"
                    Text2.Text = "Z"
            End Select


    Please remember to 'Mark as Answer' the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by frankwil96 Monday, September 30, 2013 3:48 PM
    Monday, September 30, 2013 3:42 PM
  • Thank you so much!
    Monday, September 30, 2013 3:48 PM
  • Any tips on how to add them together and display them now?

    Monday, September 30, 2013 3:51 PM
  • what do you mean by 'add them together and display them now'?

    Please remember to 'Mark as Answer' the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, September 30, 2013 3:57 PM
  • For instance, if the user puts in ABC

    A=1, so B = 2, and C = 3,

    so it would show a message that says

    6

    Monday, September 30, 2013 3:59 PM
  • So you need to Change it to

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim x As New Dictionary(Of Char, Integer)
            Dim Total As Integer
    
    
            For d As Integer = 1 To 26
                x.Add(Chr(d + 64), d)
            Next
    
            For i As Integer = 0 To txtFN.Text.ToCharArray().Length - 1
                Total += x(txtFN.Text.ToCharArray()(i))
            Next
    
            Text2.Text = Total.ToString()
    
        End Sub


    Please remember to 'Mark as Answer' the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    • Edited by Ahmed-Samy Monday, September 30, 2013 4:30 PM
    • Marked as answer by frankwil96 Wednesday, October 02, 2013 3:35 PM
    Monday, September 30, 2013 4:30 PM
  • In addition to Ahmed who already gave IMO a helpful approach, this was my example:

          Dim result = Aggregate c In txtFN.Text _
                       Select Asc(c) - 64 _
                       Into Sum()
    

    It is not better but just to give an impression how small it can be. (no input validation involved)

    Oh, and, BTW, add Option Strict On at the top of the file.



    Armin

    • Proposed as answer by Ahmed-Samy Monday, September 30, 2013 7:57 PM
    Monday, September 30, 2013 5:00 PM
  • My $.02

        Function letterToNum(name As String) As Integer
            If name.Length = 0 Then
                Return 0
            End If
    
            Dim possibleLetters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            Dim rv As Integer = possibleLetters.IndexOf(name.Substring(0, 1))
            Return rv + 1
        End Function
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            Dim fname As String = "Alpha"
            Dim mname As String = "Bravo"
            Dim lname As String = "Charlie"
            Dim n As Integer = letterToNum(fname) + letterToNum(mname) + letterToNum(lname)
            Debug.WriteLine(n)
        End Sub
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein

    Monday, September 30, 2013 5:26 PM
  • I added some code to Ahmed-Samy's post to verify that the textbox input was valid.  Here it is:

        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim x As New Dictionary(Of Char, Integer)
            Dim Total As Integer
    
            Dim mystr As String = TextBox1.Text.ToUpper()
            For Each ch As Char In mystr
                If Not Char.IsLetter(ch) And Not ch = " "c Then
                    MessageBox.Show("Incorrect entry - must be all letters", ch.ToString)
                    TextBox1.Clear()
                    TextBox1.Focus()
                    Exit Sub
                End If
            Next
            mystr = mystr.Replace(" ", "")
    
            For d As Integer = 1 To 26
                x.Add(Chr(d + 64), d)
            Next
    
            For i As Integer = 0 To mystr.ToCharArray().Length - 1
                Total += x(mystr.ToCharArray()(i))
            Next
    
            TextBox2.Text = Total.ToString()
        End Sub

    or, for Armin's post:

        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim x As New Dictionary(Of Char, Integer)
            Dim result As Integer
    
            Dim mystr As String = TextBox1.Text.ToUpper()
            For Each ch As Char In mystr
                If Not Char.IsLetter(ch) And Not ch = " "c Then
                    MessageBox.Show("Incorrect entry - must be all letters", ch.ToString)
                    TextBox1.Clear()
                    TextBox1.Focus()
                    Exit Sub
                End If
            Next
            mystr = mystr.Replace(" ", "")
    
            result = Aggregate c In mystr _
                      Select Asc(c) - 64 _
                      Into Sum()
            TextBox2.Text = result.ToString
    
        End Sub


    • Edited by Solitaire Monday, September 30, 2013 7:00 PM
    • Marked as answer by frankwil96 Wednesday, October 02, 2013 3:34 PM
    Monday, September 30, 2013 6:50 PM
  • Thank all of you, so much! This was a HUGE help.
    Wednesday, October 02, 2013 3:34 PM