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.