为什么If-Else语句不会跳转到其他语句?

时间:2014-10-01 07:41:50

标签: vb.net if-statement conditional-statements

我有问题,我还没看到。我想它与andalso或IsNot运算符有关。

因此,对于shure,我的单元格值(o)是" ImportName"但它却不会跳到其他地方。 它试图改变.cells(1)

的值
If row.Cells(0).Value IsNot "ImportName" AndAlso row.Cells(0).Value IsNot "ID" AndAlso row.Cells(0).Value IsNot "CSV_Datensatznummer" AndAlso row.Cells(0).Value IsNot "ist_headerzeile" AndAlso row.Cells(0).Value IsNot "Einlese_Datum" AndAlso row.Cells(0).Value IsNot "Dubletten_Vorschlaege" _
           AndAlso row.Cells(0).Value IsNot "forcerequestid" AndAlso row.Cells(0).Value IsNot "asp_Update_erfolgreich" AndAlso row.Cells(0).Value IsNot "adress_korrektur_noetig" AndAlso row.Cells(0).Value IsNot "adress_korrektur_abgeschlossen" AndAlso row.Cells(0).Value IsNot "in_Adrium_angelegt" AndAlso row.Cells(0).Value IsNot "fehler_bei_der_anlage" _
           AndAlso row.Cells(0).Value IsNot "import_abgeschlossen" AndAlso row.Cells(0).Value IsNot "asp_dubletten_vorschlaege" AndAlso row.Cells(0).Value IsNot "asp_forcerequerstid" AndAlso row.Cells(0).Value IsNot "asp_fehler_bei_der_anlage" AndAlso row.Cells(0).Value IsNot "asp_in_Adrium_angelegt" AndAlso row.Cells(0).Value IsNot "asp_import_abgeschlossen" AndAlso row.Cells(0).Value IsNot "Update_erfolgreich" _
           AndAlso row.Cells(0).Value IsNot "Fehler_Update" AndAlso row.Cells(0).Value IsNot "asp_Update_erfolgreich" AndAlso row.Cells(0).Value IsNot "asp_Fehler_Update" AndAlso row.Cells(0).Value IsNot "anreichern_fehler" AndAlso row.Cells(0).Value IsNot "asp_anreichern_fehler" AndAlso row.Cells(0).Value IsNot "Dateiname" AndAlso row.Cells(0).Value IsNot "addresssource_val" AndAlso row.Cells(0).Value IsNot "asp_addresssource_val" Then

                            row.Cells(1).Value = Startseite.Spaltennamen(i)
                        Else
                            MsgBox("Yolo why it doesnt work")

                        End If

1 个答案:

答案 0 :(得分:3)

  

Cells(0)的值是" ImportName"但它却没有跳跃   进入Else

IsNot operator确定两个对象引用是否引用不同的对象。但是,不执行值比较。如果您想与非空值进行比较,只需使用=<>

Dim value As String = row.Cells(0).Value
If value <> "ImportName" AndAlso value <> "ID" AndAlso value <> "CSV_Datensatznummer" AndAlso value <> "ist_headerzeile" AndAlso value <> "Einlese_Datum" AndAlso value <> "Dubletten_Vorschlaege" _ 
'...'

我仅对Is使用IsNotNothing

通过使用集合和Enumerable.Contains,您可以使上面的代码更具可读性和可维护性。还要考虑.NET是区分大小写的,这里是一个不敏感的比较:

Dim values = { "ImportName", "CSV_Datensatznummer", "ist_headerzeile", "Einlese_Datum", "Dubletten_Vorschlaege" }
If values.Contains(row.Cells(0).Value, StringComparer.CurrentCultureIgnoreCase) Then
    ' ... '
Else
    ' ... '
End If