首页 > 解决方案 > 即使 sring 不同,也会进入状态

问题描述

我是 VB.NET 的初学者。如果字符串中的数字符合很少的条件,我正在遍历车辆列表并将 STRING 中的索引添加到另一个列表中。

第一个条件;If Not item.Equals("11652") 即使不为真,仍然进入状态;

等于(),不等于(),Tostring.Equals

Dim cpti = 0

For Each item In Vehicules.Items
    If ex = 1 Then
        If Not item.Equals("11652") Or item.ToString() <> "11785" Or item.ToString() <> "11814" Or item.ToString() <> "11852" Or item.ToString() <> "11853" Then
            list.Add(cpti)
        End If             

        If item.ToString() = "530011" Or item.ToString() = "530012" Or item.ToString() = "530013" Or item.ToString() = "530014" Or item.ToString() = "530015" Or item.ToString() = "530016" Or item.ToString() = "530017" Or item.ToString() = "530018" Or item.ToString() = "530019" Or item.ToString() = "530020" Then
            list.Add(cpti)
        End If  
    ElseIf ex = 3 Then
        If item.ToString() <> "326481" Or item.ToString() <> "326483" Or item.ToString() <> "326556" Or item.ToString() <> "326557" Then
            list.Add(cpti)
        End If
    Else
        liste.Add(cpti)
    End If

    cpti = cpti + 1
Next

标签: stringvb.netif-statementequals

解决方案


让我们走这条线

If item <> "11652" Or item <> "11785" Then

这将始终等于 true。让我们尝试一些例子

item = "1"
If item <> "11652" Or item <> "11785" Then
If True Or True Then ' Both of them are True, go in the If

item = "11652"
If item <> "11652" Or item <> "11785" Then
If False Or True Then ' One of them is true, go in the If

item = "11785"
If item <> "11652" Or item <> "11785" Then
If True Or False Then ' One of them is true, go in the If

您可能想做 AND 而不是 OR。但我不知道你想要做什么背后的逻辑,所以我不能给你一个直接的答案。


推荐阅读