Search code examples
vb.netfor-loopmessagebox

Conversion from string type long error message box


Rewriting an old program that was done in vba in vb. I have a for loop that checks to make sure a dropdown selection doesn't match other ddl selections. I am getting 'Conversion from String to type 'Long' is not valid' on the two MessageBox.Show lines, I have tried moving the And and & all around and still get the error. The commented out part is what I had in vba. How should the syntax be?

 Dim TCi(0 To 3) As String
        TCi(1) = "Air Temperature"
        TCi(2) = "Out Temperature(A)"
        TCi(3) = "Inlet"

        Dim TCj(0 To 3) As String
        TCj(1) = "Air Temperature"
        TCj(2) = "Out Temperature(A)"
        TCj(3) = "Inlet"

        SelectionFlagA = 0
        SelectionFlagB = 0
        TCSflag = 0
        For i = 1 To 3
            TCChannel = Me.Controls.Item("TCSDropDown" & i).Text
            If TCChannel = "Channel_Not_Available" Then
            ElseIf TCChannel = "Select Temp" Then
                TCSflag = TCSflag + 1
                'This code works
                MessageBox.Show("You selected the same temp for both" & TCi(i) & " and " & TCj(j), "Conflicting Temp Selection!", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit For
            Else
                If InStr(1, TCChannel, "A") Then
                    SelectionFlagA = 1
                End If
                If InStr(1, TCChannel, "B") Then
                    SelectionFlagB = 1
                End If

                For j = 1 To 3

                    If TCi(i) <> TCj(j) Then
                        TCChannel1 = Me.Controls.Item("TCSDropDown" & j).Text
                        If TCChannel1 = "Channel_Not_Available" Then
                        Else
                            If TCChannel = TCChannel1 Then
                                'This code works
                                MessageBox.Show("You selected the same temp for both " & TCi(i) And "" & TCj(j), vbCritical, "Conflicting Temp Selection!")
                                'VBA Line: MsgBox "You selected the same temp for both " & TCi(i) & " and " & TCj(j), vbCritical, "Conflicting Temp Selection!"
                                TCSflag = TCSflag + 1
                                Exit For
                            End If
                        End If
                    End If
                Next
                If TCSflag >= 1 Then
                    Exit For
                End If
            End If


        Next

Solution

  • You should be able to use exactly what the VBA code has

    MessageBox.Show("Please select a temp for" & " " & TCi(i), "Error")
    

    That said, there is no real point in concatenating a space after ... a temp for like that. Just add it after the for in the actual string.

    MessageBox.Show("Please select a temp for " & TCi(i), "Error")