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:

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.

@Minimalist
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.