列表中的 VB.NET If-Else

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6727949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:36:58  来源:igfitidea点击:

VB.NET If-Else in List

vb.netlistconditional-statementsif-statement

提问by dpp

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:

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

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 ToStatusin the future? It will be a nightmare! Now, if such functionality exists, how can I add "And" and "Or" in the statement?

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

Thanks in advance!

提前致谢!

回答by SLaks

You can use the Containsfunction:

您可以使用该Contains功能:

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

回答by Petr Moravek

if {"1CE","2TL","2PM"}.Contains(ToStatus)

if {"1CE","2TL","2PM"}.Contains(ToStatus)

then ..... End I

然后……结束我

回答by jusdoo

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

回答by KyleMit

Like Slaks pointed out, you can use Containson an enumerablecollection. But I think readability suffers. I don't care if some list containsmy variable; i want to know if my variable is insome list. You can wrap contains in an extension method like so:

就像Slaks 指出的那样,您可以Containsenumerable集合上使用。但我认为可读性会受到影响。我不在乎某个列表是否包含我的变量;我想知道我的变量是否某个列表中。您可以将 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

Then call like this:

然后像这样调用:

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

回答by dpp

For .NET 2.0

对于 .NET 2.0

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

我遇到了另一个 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 - Alternative to Array.Contains?

VB.NET - Array.Contains 的替代品?

回答by Iladarsda

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