BC30452:使用整数变量和整数数组的'='运算符问题 - VB Visual Basic

问题描述 投票:1回答:1

我是Visual Basic的新手,没有任何经验,除了使用办公室宏(由他人而不是我自己制作)并且在过去1-2天内自己崩溃,所以请原谅我的问题的基本性质。

我遇到了一个任务,我需要在CrystalReports中使用VB来制作简单的公式来决定填充公式包含字段的信息。我目前无法在我的系统上访问CrystalReports,所以我将循环/决策测试为VBScripts,看看它们是否有效。

到目前为止,我有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'='没有为类型'Integer'和'Integer()'定义。”

说实话,我不确定为什么我收到这个,“Integer”和“Integer()”之间的区别在于它们阻止它们与'='运算符一起使用。

我一直在尝试使它工作,但由于缺乏VB的知识(并且不知道我的问题的相关搜索术语),我的大部分尝试都缺乏有用的方向。

我有这个循环的另一个'版本',而不是使用数组我直接使用...将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只是注意到没有“Console.WriteLine”等在输入到CrystalReports的代码中,也没有用户输入。写入函数和输入将由数据库引用填充。

arrays vb.net visual-studio vbscript crystal-reports
1个回答
2
投票

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

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

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

但是,Contains实际上是LINQ提供的扩展方法,因此为了使用它,您需要导入System.Linq命名空间。如果您不想(或不能)使用LINQ,那么您可以使用Array.IndexOf方法:

If Array.IndexOf(arrayset1, userinput1) >= 0 Then
    ' ...
© www.soinside.com 2019 - 2024. All rights reserved.