mehrere IF-Abfragen Denkhilfe benötigt

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 5 Antworten in diesem Thema. Der letzte Beitrag () ist von zauber777.

    mehrere IF-Abfragen Denkhilfe benötigt

    Ich habe mehrere Zustände, die abgefragt werden sollen.
    Nur wurde mir mal gesagt, dass man nicht zu viele IF-Abfragen in einander verketten sollte <-- dies wäre es jedoch in meinem Fall.

    VB.NET-Quellcode

    1. If Attribut1 IsNot Nothing Or Attribut2 IsNot Nothing Or Attribut3 IsNot Nothing Then
    2. 'wenn alle attribute gefüllt sind, dann das
    3. Else
    4. If Attribut1 IsNot Nothing Or Attribut2 IsNot Nothing Or Attribut3 Is Nothing Then
    5. 'wenn Attribut1 und Attribut2 Daten hat und Attribut 3 leer ist, dann das
    6. Else
    7. If Attribut1 IsNot Nothing Or Attribut2 Is Nothing Or Attribut3 Is Nothing Then
    8. 'wenn nur Attribut1 Daten hat und Attribut2 + 3 leer ist, dann das
    9. Else
    10. 'wenn alle Attribute leer, dann das
    11. End If
    12. End If
    13. End If


    Ist dies der richtige Weg oder gibt es eine bessere Lösung, wenn ja wie?

    zauber777 schrieb:

    gibt es eine bessere Lösung, wenn ja wie?
    Versuche es mit ElseIf

    VB.NET-Quellcode

    1. If Attribut1 IsNot Nothing Or Attribut2 IsNot Nothing Or Attribut3 IsNot Nothing Then
    2. 'wenn alle attribute gefüllt sind, dann das
    3. ElseIf Attribut1 IsNot Nothing Or Attribut2 IsNot Nothing Or Attribut3 Is Nothing Then
    4. 'wenn Attribut1 und Attribut2 Daten hat und Attribut 3 leer ist, dann das
    5. ElseIf Attribut1 IsNot Nothing Or Attribut2 Is Nothing Or Attribut3 Is Nothing Then
    6. 'wenn nur Attribut1 Daten hat und Attribut2 + 3 leer ist, dann das
    7. Else
    8. 'wenn alle Attribute leer, dann das
    9. End If
    @zauber777 Da die Attribut1-Abfrage überall gleich ist, würde sie im True-Fall bereits beim ersten If abgearbeitet, Deine Logik ist nicht logisch.
    Kann es sein, dass Du nicht Or / OrElse meinst, sondern And / AndAlso?
    Deine verbale Beschreibung impliziert dies.

    VB.NET-Quellcode

    1. If Attribut1 IsNot Nothing AndAlso Attribut2 IsNot Nothing AndAlso Attribut3 IsNot Nothing Then
    2. ElseIf Attribut1 IsNot Nothing AndAlso Attribut2 IsNot Nothing AndAlso Attribut3 Is Nothing Then
    3. ElseIf Attribut1 IsNot Nothing AndAlso Attribut2 Is Nothing AndAlso Attribut3 Is Nothing Then
    4. End If

    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!