Hey there guys, Im looking for a nifty trick in VB.NET that will help me with an error that is constantly Reoccuring.
This is some example code:

Dim array as Textbox() = {txt1, txt2, txt3}
'Array of textbox's

For loop = 0 to 3
    If Array(Loop).text = Data_In_Database then
    'Do something
next

Here I have a loop that will Do something if some data in my array of textbox's matches something in a database.
What I want to know is how can I place an error box if it Doesnt Match?

If I set it after the loop, it will happen even if the data does match.
If I set it as a fail condition in the IF statement, then I will get it appearing for each textbox that fails.

The only thing i can think of is the make a variable to count along with the loop and then if it fails on that count, show an error box, but that can be quite innefficient in comparison to an easier solution that i'm sure someone out there knows.

Thanks for the help guys c:

Dani AI

Generated

Short answer: check the whole set once, then show a single error if none matched. is right about zero-based arrays; ’s boolean-flag idea works. Two cleaner alternatives that avoid per-item message boxes and off-by-one bugs are shown below.

A concise LINQ approach (compact, reads well):

Dim boxes = New TextBox() {txt1, txt2, txt3}
Dim value = Data_In_Database.Trim()

If boxes.Any(Function(tb) String.Equals(tb.Text.Trim(), value, StringComparison.OrdinalIgnoreCase)) Then
    Dim matched = boxes.FirstOrDefault(Function(tb) String.Equals(tb.Text.Trim(), value, StringComparison.OrdinalIgnoreCase))
    ' process the matched TextBox (matched IsNot Nothing)
Else
    MessageBox.Show("Value not found")
End If

If you cannot or prefer not to use LINQ, use a For Each with an early exit to avoid multiple dialogs:

Dim matchedBox As TextBox = Nothing
For Each tb As TextBox In boxes
    If String.Equals(tb.Text.Trim(), value, StringComparison.OrdinalIgnoreCase) Then
        matchedBox = tb
        Exit For
    End If
Next

If matchedBox IsNot Nothing Then
    ' do something with matchedBox
Else
    MessageBox.Show("Value not found")
End If

Troubleshooting notes and best practices:

  • Prefer For Each to avoid index off-by-one mistakes (see ). If you use index loops, loop 0 To boxes.Length - 1.
  • Normalize strings (Trim, consistent case) or use StringComparison to avoid false negatives.
  • For many comparisons or many DB values, pull DB values into a HashSet and test Contains for O(1) checks.
  • Avoid showing MessageBox inside the loop; show it only after the loop or when no match is found.

Recommended Answers

All 4 Replies

Take a Boolean varable before declaring the for loop.
Your codes Should be like

Dim array as Textbox() = {txt1, txt2, txt3}
'Array of textbox's
Dim match As Boolean = False
For loop = 0 to 3
    If Array(Loop).text = Data_In_Database then
    'Do something
    match = True
    Endif
next

If Not match Then MessageBox.Show("Your Error Message")

Hope it should help you.

An array starts counting at 0. So your llop shoild be:

For loop = 0 to 2
    If Array(Loop).text = Data_In_Database then
    'Do something
next

whic gives you the indexes 0, 1,2, = 3 items

@ shark 1
maybe you can explain to the forum why you vote me down if you made the mistake. I voted you down because you replicated the mistake in the first place.


Truely sorry, I overlooked it. I did it by mistake. I took it back.

I did not tried to point out to the mistake of the structure of the loop. Because I mean it was a sample codes for discussion.

My view was how he can generate the error message.
Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.