Conversion from string “a” to type 'Boolean' is not valid

房东的猫 提交于 2019-12-11 04:07:17

问题


This one's really got me confused. I'm trying to do a visual basic console application that if the user enters 'A' or 'a' then the program should do 'x', but that's not working. The error I get is:

Conversion from string "a" to type 'Boolean' is not valid.

Here's my code:

Module Module1

Sub Main()
    Dim Selection As String

    Console.WriteLine("Please select your function:")
    Console.WriteLine("* To convert binary to decimal, press A,")
    Console.WriteLine("* Or to convert decimal to binary, press B")

    Selection = Console.ReadLine

    If Selection = "A" Or "a" Then
        Console.WriteLine("This will be A")
    ElseIf Selection = "B" Or "b" Then
        Console.WriteLine("This will be B")
    ElseIf Selection = Not "A" Or "a" Or "B" Or "b" Then
        Console.WriteLine("Please try again")
        Do Until Selection = "A" Or "a" Or "B" Or "b"
        Loop
    End If


End Sub

What should be the correct usage of the Or in this piece of code to make it function correctly?

Thank you,

Jake


回答1:


Your If clauses should be like:

If Selection = "A" Or Selection = "a" Then

The clauses between the Or should each be boolean expressions, and "a" is simply a character, not a boolean expression.




回答2:


I would suggest changing the string entered into upper case before the if statements. Also the last else if statement isn't needed and can be replaced with a single else.

Sub Main()
    Dim Selection As String

    Console.WriteLine("Please select your function:")
    Console.WriteLine("* To convert binary to decimal, press A,")
    Console.WriteLine("* Or to convert decimal to binary, press B")

    Selection = Console.ReadLine.ToUpper() 'Not tested otherwise use the one below
'Selection=Selection.ToUpper()

    If Selection = "A" Then
        Console.WriteLine("This will be A")
    ElseIf Selection = "B"  Then
        Console.WriteLine("This will be B")
    Else
        Do Until Selection = "A"  Or Selection = "B" 
               'Loop Code goes here
        Loop
    End If


End Sub



回答3:


Using Select Case is yet another alternative:

Sub Main()
    Console.WriteLine("Please select your function:")
    Console.WriteLine("* To convert binary to decimal, press A,")
    Console.WriteLine("* Or to convert decimal to binary, press B")
    Console.WriteLine("* To Quit, press Q")
    Dim Selection As String = ValidateInput()
    If Selection <> "Q" Then
        'do something
    End If
End Sub

Function ValidateInput() As String

    Dim Selection As String
    Selection = Console.ReadLine

    Select Case Selection.ToUpper
        Case "A"
            Console.WriteLine("This will be A")
        Case "B"
            Console.WriteLine("This will be B")
        Case "Q"
            Return "Q"
        Case Else
            Console.WriteLine("Please try again")
            ValidateInput()
    End Select
    Return Selection

End Function


来源:https://stackoverflow.com/questions/13128753/conversion-from-string-a-to-type-boolean-is-not-valid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!