VB.NET - 如何在条件中使用数组文字?

发布于 2024-11-27 05:05:43 字数 627 浏览 3 评论 0原文

我刚刚学习如何在 VB.NET 中创建数组文字。

Dim MyArray = New Integer() { 1, 2, 3 }
' Or
Dim MyArray() As Integer = { 1, 2, 3 }
' Or
Dim MyArray() = { 1, 2, 3 }
' Or
Dim MyArray() = { 1, 2, "A", "B" }

现在,我想在条件中使用文字数组(请参阅伪代码),

If 1 exists in {1,2,3,4} Then
    MsgBox "Exists!"
End If

但我不知道如何操作,似乎您必须先将其分配给变量,然后才能在条件中使用它。

    Dim MyArray() As Integer = {3, 2, 3}
    If (MyArray.Contains(1)) Then
        MsgBox("exists!")
    Else
        MsgBox("does not exist!")
    End If

上面的代码有效,但我只是想知道是否有任何方法可以在不先将数组文字分配给变量的情况下执行此操作?

提前致谢!

I just learn how to create an array literal in VB.NET.

Dim MyArray = New Integer() { 1, 2, 3 }
' Or
Dim MyArray() As Integer = { 1, 2, 3 }
' Or
Dim MyArray() = { 1, 2, 3 }
' Or
Dim MyArray() = { 1, 2, "A", "B" }

Now, I want to use A LITERAL ARRAY in a condition (see pseudo-code)

If 1 exists in {1,2,3,4} Then
    MsgBox "Exists!"
End If

but I don't know how, seems like you have to assign it to a variable before you can use it in the condition.

    Dim MyArray() As Integer = {3, 2, 3}
    If (MyArray.Contains(1)) Then
        MsgBox("exists!")
    Else
        MsgBox("does not exist!")
    End If

The above code works, but I'm just wondering is there any way to do this without assigning the array literal to variable first?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぶ宁プ宁ぶ 2024-12-04 05:05:43

为此,请使用 {1,2,3,4}.Contains(1)

Use {1,2,3,4}.Contains(1) for this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文