vb.net 从字符串“a”到类型“Boolean”的转换无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13128753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 10:56:52  来源:igfitidea点击:

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

vb.netconsole-application

提问by Jake Andrew

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:

这个真的把我搞糊涂了。我正在尝试做一个可视化基本控制台应用程序,如果用户输入“A”或“a”,那么程序应该执行“x”,但这不起作用。我得到的错误是:

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

从字符串“a”到类型“Boolean”的转换无效。

Here's my code:

这是我的代码:

Module Module1

模块模块1

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?

这段代码中 Or 的正确用法应该是什么才能使其正常运行?

Thank you,

谢谢,

Jake

Hyman

回答by Oded

Your Ifclauses should be like:

你的If条款应该是这样的:

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

The clauses between the Orshould eachbe boolean expressions, and "a"is simply a character, not a boolean expression.

之间的条款Or每个是布尔表达式,并且"a"仅仅是一个字符,而不是一个布尔表达式。

回答by Gage

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.

我建议在 if 语句之前将输入的字符串更改为大写。也不需要最后一个 else if 语句,可以用单个 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

回答by ron tornambe

Using Select Caseis yet another alternative:

使用Select Case是另一种选择:

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