Dillinger
Dillinger

Reputation: 1903

How to check if a multiple variable are empty?

I know how to check if variable is empty or not, but what I'm trying to do is display a message on the empty variable, for example:

    Dim server_name = TextBox1.Text
    Dim username = TextBox2.Text
    Dim password = TextBox3.Text
    Dim database = TextBox4.Text

    If server_name.Length > 0 And username.Length > 0 And password.Length > 0 And database.Length > 0 Then

    End If

How you can see I've a multiple condition, so if server_name is empty I want display a message

Server name field isn't filled correctly

It's possible do this without an else if?

Upvotes: 0

Views: 1868

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460028

I would use an If... even if you don't like it. However, if you want something "more elegant" you could use a collection and Linq:

Dim allFields = New List(Of KeyValuePair(Of String, String)) From {
    New KeyValuePair(Of String, String)("Server name", server_name),
    New KeyValuePair(Of String, String)("User name", username),
    New KeyValuePair(Of String, String)("Password", password),
    New KeyValuePair(Of String, String)("Database", database)
}

Dim invalids = From kv In allFields
               Where String.IsNullOrEmpty(kv.Value) 

For Each invalid In invalids
    Console.WriteLine("{0} field isn't filled correctly", invalid.Key)
Next

Just another question, it's possible set the foreground color red to the empty field in the foreach?

Yes, then you need to store the TextBox instead of only it's Text as Value. Then you can set the color of the TextBox in the loop:

Dim allFields As New List(Of KeyValuePair(Of String, TextBox)) From {
    New KeyValuePair(Of String, TextBox)("Server name", TextBox1),
    New KeyValuePair(Of String, TextBox)("User name", TextBox2),
    New KeyValuePair(Of String, TextBox)("Password", TextBox3),
    New KeyValuePair(Of String, TextBox)("Database", TextBox4)
}

Dim invalids = From kv In allFields
               Where String.IsNullOrEmpty(kv.Value.Text) 

For Each invalid In invalids
    Console.WriteLine("{0} field isn't filled correctly", invalid.Key)
    invalid.Value.ForeColor = Drawing.Color.Red
Next

Note that this has another advantage: you could even declare this list as a field in your class(form). So you only need to declare and initialize it once. If you execute the LINQ query at the For Each it will automatically evaluate it with the current values in the textboxes, not the initial ones when the list was initialized. Side-note: you should use more meaningful names like TxtPassword instead of TextBox3.

Upvotes: 4

Anax
Anax

Reputation: 9362

There are many ways to do this, I'll give you an example:

Dim s As String() = {TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text}
If String.Join("", s).Length > 0 Then
    ' program logic
End If

Effectively, you are creating an array of strings and then you join them and check the final string's length.

Upvotes: 2

Related Questions