Überprüfen ob String wirklich nothing ist

  • VB.NET

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von Keybladmaster.

    Überprüfen ob String wirklich nothing ist

    Huhu liebe Gemeinde,

    ich nutze folgende Abfrage für Input:

    VB.NET-Quellcode

    1. Dim myValue As String = InputBox("Enter login:", "Enter login:", "login")
    2. if not String.IsNullOrEmpty(myValue) = true or not myvalue = "" or not myvalue = nothing
    3. 'TUETWAS
    4. End If


    Wenn ich auf Abbrechen klicke, dann ist der String "myValue" nothing (Laut MSGBOX wird myvalue mit nichts angezeigt). Obwohl ich die Nothing Abfrage mache, wird die If-Schleife trotzdem fortgesetzt.

    Weiß jemand, wie ich die If-Abfrage richtig mache?

    Vielen Dank im Voraus! :)
    Naja, die If-Abfrage ist so konzipiert, dass sie immer richtig ist. Wenn der String ​Nothing ist, dann ist ​not myvalue = "" richtig, wenn ​"" drinsteht, dann ist ​not myvalue = nothing richtig.
    Du solltest hier viel ändern. Als erstes solltest Du von der InputBox wegkommen, da die ein VB6-Relikt und ranzig ist. Mach dafür eigene Dialoge. ​MsgBox -> ​MessageBox.Show.

    Dann verstehe ich Deine Abfrage auch nicht, da ​String.IsNullOrEmpty(myValue) ja schon Deine hinteren beiden Bedingungen prüft. Das ist alles doppelt gemoppelt und hebt sich somit im Endeffekt gegenseitig auf.
    Wenn Du von der InputBox wegkommst, dann sparst Du Dir sowieso den ganzen Krempel und es ist weitaus sauberer.

    Grüße
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!:
    Hier ein kleiner Tipp für deine Inputbox.


    VB.NET-Quellcode

    1. Public Class ext_inputbox
    2. Sub New(ByVal prompt As String, ByVal title As String, ByVal defaultresponse As String,
    3. Optional ByVal usePasswordChar As Boolean = False)
    4. InitializeComponent()
    5. If usePasswordChar Then
    6. TextBox1.PasswordChar = PasswordChar
    7. End If
    8. Me.CancelButton = btAbbrechen 'Wird ausgelöst, wenn ESC gedrückt wird
    9. Me.AcceptButton = btOK 'wird bei OK ausgelöst
    10. Label1.Text = prompt
    11. TextBox1.Text = defaultresponse
    12. Me.Text = title
    13. Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog
    14. Me.ShowDialog()
    15. End Sub
    16. Private _title As String
    17. Public Property Title() As String
    18. Get
    19. Return _title
    20. End Get
    21. Set(ByVal value As String)
    22. _title = value
    23. End Set
    24. End Property
    25. Private _prompt As String
    26. Public Property Prompt() As String
    27. Get
    28. Return _prompt
    29. End Get
    30. Set(ByVal value As String)
    31. _prompt = value
    32. End Set
    33. End Property
    34. Private _usepasswordchar As Boolean
    35. Public Property UsePassWordChar() As Boolean
    36. Get
    37. Return _usepasswordchar
    38. End Get
    39. Set(ByVal value As Boolean)
    40. _usepasswordchar = value
    41. End Set
    42. End Property
    43. Private _passwordChar As Char = "*"c
    44. Public Property PasswordChar() As Char
    45. Get
    46. Return _passwordChar
    47. End Get
    48. Set(ByVal value As Char)
    49. _passwordChar = value
    50. End Set
    51. End Property
    52. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btOK.Click
    53. Me.DialogResult = DialogResult.OK
    54. Prompt = TextBox1.Text
    55. Me.Close()
    56. End Sub
    57. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btAbbrechen.Click
    58. Me.DialogResult = DialogResult.Cancel
    59. Prompt = ""
    60. Me.Close()
    61. End Sub
    62. End Class


    VB.NET-Quellcode

    1. Dim box As New ext_inputbox("bla", "bla", "Vorgabetext")
    2. box.ShowDialog()
    3. If box.DialogResult = DialogResult.OK Then '...
    4. 'whatever
    5. End If
    6. Dim mpassword As New ext_inputbox("Passworteingabe", "Passwort", "", True)
    7. mpassword.ShowDialog()



    Musst du dann Dementsprechend noch für dich Anpassen.

    LG
    @Keybladmaster Da fehlt aber dann die Verberung. Sollte wohl Form sein, oder?

    Grüße
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!: