BC30452:将“=”运算符与整数变量和整数数组一起使用时出现问题 - VB Visual Basic

BC30452: Issue using '=' Operator with Integer Variable & Array of Integers - VB Visual Basic

我是 Visual Basic 的新手,没有任何经验,除了使用 Office 宏(由其他人而不是我自己制作)和在过去 1-2 天里自己的速成课程之外,所以请原谅我的问题的基本性质.

我遇到了一个任务,我需要在 CrystalReports 中使用 VB 来制作简单的公式来决定将哪些信息填充到包含字段的公式中。我目前无法在我的系统上访问 CrystalReports,因此我正在测试 loops/decisions 作为 VB 脚本以查看它们是否可以工作。

到目前为止,我有 2 个工作循环,但想要第三个循环来为用户提供他们想要使用的更多选项。

下面的循环是我遇到的问题:

    Dim userinput1 As Integer = Nothing
    Dim arrayset1 = New Integer() {1, 2, 3, 4}
    Dim arrayset2 = New Integer() {5, 6, 7, 8, 9}
    Dim arrayset3 = New Integer() {10, 11, 12, 13}
    Dim arrayother = New Integer() {14} 'this is just to keep with array formatting, no real reason for the 1 value array other than that

    Console.WriteLine("Enter Value:") 
    userinput1 = Console.ReadLine() 
    Console.WriteLine("Value Entered = " & userinput1) 
    Console.WriteLine()
    Console.ReadLine()

    If userinput1 = arrayset1 Then 
        Console.WriteLine(userinput1 & "_SUFFIX1")
    ElseIf userinput1 = arrayset2 Then
        Console.WriteLine(userinput1 & "_SUFFIX2")
    ElseIf userinput1 = arrayset3 Then
        Console.WriteLine(userinput1 & "_SUFFIX3")
    ElseIf userinput1 = arrayother Then
        Console.WriteLine(userinput1 & "_SUFFIX4")
    Else
        Console.WriteLine(userinput1)
    End If

我在 Visual Studio 2017 社区中编写和测试此代码,当我 运行 代码时收到错误 "BC30452 Operator '=' is not defined for types 'Integer' and 'Integer()'."

老实说,我不确定为什么会收到此消息,"Integer" 和 "Integer ()" 之间的确切区别是阻止它们与“=”一起使用操作员。

我一直在努力让它发挥作用,但由于缺乏对 VB 的了解(并且不知道与我的问题相关的搜索词),我的大部分尝试都缺乏有用的指导。

我有另一个 'version' 这个循环,我没有使用数组,而是直接使用...

将 userinput1 与其值进行比较
if userinput1 = 1 Or userinput 1 =2 Or userinput1 = 3 Or userinput1 = 4 Then
    Console.WriteLine(userinput1 & "_SUFFIX1")
etc etc
.......

..我没问题 运行。

此时只有 14 个变量值使用 "Or" 没有太大问题,但是变量的数量有可能增加,在这种情况下,我希望保持清洁。我有另一个使用 "SelectCase" 的循环,它也很干净(呃),所以失败了我仍然有那个循环。

我的问题基本上是,我以何种方式不正确地使用了导致我出现问题的数组?或者如果我格式化代码的方式有误,那是什么?

除了我上面列出的内容之外,欢迎就我如何解决这个问题提出任何其他建议。

提前致谢, LMacs.

P.S 请注意,在输入到 CrystalReports 的代码中不会有 "Console.WriteLine" 等,也不会有用户输入。写入函数和输入将由数据库引用填充。

Integer() 是一个整数数组,而 Integer 只是一个整数。错误消息是说您不能使用 = 运算符来测试整数数组是否等于单个整数。语言不知道如何做到这一点。语言如何知道你的意思?你想看看数组是否只包含一个与另一个整数值相同的整数?还是要检查数组是否在其整数列表中包含特定值?你需要更准确地表达你的意图。

根据你的示例,我假设你想查看数组是否包含输入的整数。为此,您可以使用数组的 Contains 方法:

If arrayset1.Contains(userinput1) Then 
    ' ...

不过,Contains其实是LINQ提供的扩展方法,所以要使用它,需要导入System.Linq命名空间。如果您不想(或不能)使用 LINQ,则可以改用 Array.IndexOf 方法:

If Array.IndexOf(arrayset1, userinput1) >= 0 Then
    ' ...