VB.NET 列表中的 If-Else

发布于 2024-11-24 19:41:52 字数 336 浏览 3 评论 0原文

我只是想知道 VB.NET 中是否有一种方法可以查找列表中是否存在特定值或可以在我的 If-else 条件中使用的值。我现在正在做的是使用这个:

If ToStatus = "1CE" Or ToStatus = "2TL" Or ToStatus = "2PM" Then
    'Do something
Else
    'Do something
End If

这工作得很好,但是如果我将来有数百个字符串要与 ToStatus 进行比较怎么办?这将是一场噩梦!现在,如果存在这样的功能,我如何在语句中添加“And”和“Or”?

提前致谢!

I just want to know if there's an approach in VB.NET that can find if a specific value exist on a list or something which can use in my If-else condition. What I'm doing now is to use this:

If ToStatus = "1CE" Or ToStatus = "2TL" Or ToStatus = "2PM" Then
    'Do something
Else
    'Do something
End If

This works fine, but how if I have hundreds of string to compare to ToStatus in the future? It will be a nightmare! Now, if such functionality exists, how can I add "And" and "Or" in the statement?

Thanks in advance!

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

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

发布评论

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

评论(6

開玄 2024-12-01 19:41:52

您可以使用 Contains 函数:

Dim someList = New List(Of String) From { ... }
If Not someList.Contains(ToStatus) Then

You can use the Contains function:

Dim someList = New List(Of String) From { ... }
If Not someList.Contains(ToStatus) Then

您可以执行以下操作:

If {"1CE","2TL","2PM"}.Contains(ToStatus) Then
    ' ...
End If

You can do the following:

If {"1CE","2TL","2PM"}.Contains(ToStatus) Then
    ' ...
End If
幼儿园老大 2024-12-01 19:41:52

就像 Slaks 指出的一样,您可以在 enumerable 上使用 Contains收藏。但我认为可读性会受到影响。我不在乎某个列表是否包含我的变量;我想知道我的变量是否在某个列表中

您可以将 contains 包装在扩展方法中,如下所示:

Imports System.Runtime.CompilerServices
Module ExtensionMethods

    <Extension()> _
    Public Function [In](Of T)(ByVal item As T, ByVal ParamArray range() As T) As Boolean
        Return range.Cast(Of T).Contains(item)
    End Function

End Module

然后像这样调用:

If ToStatus.In("1CE","2TL","2PM") Then

Like Slaks pointed out, you can use Contains on an enumerable collection. But I think readability suffers. I don't care if some list contains my variable; I want to know if my variable is in some list.

You can wrap contains in an extension method like so:

Imports System.Runtime.CompilerServices
Module ExtensionMethods

    <Extension()> _
    Public Function [In](Of T)(ByVal item As T, ByVal ParamArray range() As T) As Boolean
        Return range.Cast(Of T).Contains(item)
    End Function

End Module

Then call like this:

If ToStatus.In("1CE","2TL","2PM") Then
萤火眠眠 2024-12-01 19:41:52

你可以使用选择案例

select case A
   case 5,6,7,8
       msgbox "you are in"
   case else
       msgbox "you are excluded"
end select

you may use select case

select case A
   case 5,6,7,8
       msgbox "you are in"
   case else
       msgbox "you are excluded"
end select
余生共白头 2024-12-01 19:41:52

对于 .NET 2.0

我遇到了 SLaks 解决方案不起作用的另一个问题,即如果您使用 .NET 2.0,其中方法 Contains 不存在。所以这是解决方案:

If (Array.IndexOf(New String() {"1CE", "2TL", "2PM"}), ToStatus > -1) Then
    'Do something if ToStatus is equal to any of the strings
Else
    'Do something if ToStatus is not equal to any of the strings
End If

VB.NET - Array.Contains 的替代方案?

For .NET 2.0

I came across another problem where SLaks solution won't work, that is if you use .NET 2.0 where method Contains is not present. So here's the solution:

If (Array.IndexOf(New String() {"1CE", "2TL", "2PM"}), ToStatus > -1) Then
    'Do something if ToStatus is equal to any of the strings
Else
    'Do something if ToStatus is not equal to any of the strings
End If

VB.NET - Alternative to Array.Contains?

辞取 2024-12-01 19:41:52

从列表中删除重复项

Dim ListWithoutDuplicates As New List(Of String)
For Each item As String In ListWithDuplicates

    If ListWithoutDuplicates.Contains(item) Then

     ' string already in a list - do nothing

    Else

        ListWithoutDuplicates.Add(item)

    End If

Next

Remove duplicates from the list

Dim ListWithoutDuplicates As New List(Of String)
For Each item As String In ListWithDuplicates

    If ListWithoutDuplicates.Contains(item) Then

     ' string already in a list - do nothing

    Else

        ListWithoutDuplicates.Add(item)

    End If

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