VB.NET - 如何在条件中使用数组文字?
我刚刚学习如何在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为此,请使用
{1,2,3,4}.Contains(1)
。Use
{1,2,3,4}.Contains(1)
for this.