Visual basic Loop

jkresh

Platinum Member
Jun 18, 2001
2,436
0
71
you while loop continues until 30, it doesn't end when "Logged in" appears, and once it exits the loop it sets it to "Incorrect Username" so that is what you will always have.
I havent used vb in years but I think this should work (it might be & or && instead of and, and possibly not instead of ! but you can fix the semantics). Lines with changes are bold.


Private Sub cmd_login_Click()
usernum = 1
Do While usernum <= 30 and txt_name.Text != username(usernum)
If txt_name.Text = username(usernum) Then
MsgBox "Logged in"
Else
usernum = usernum + 1
End If
Loop
If txt_name.Text != username(usernum) Then
MsgBox "Incorrect Username"
End If
End Sub

 

jkresh

Platinum Member
Jun 18, 2001
2,436
0
71
this is messy but should work, the prior code had a problem because vb.net does something stupid with and, with most languages if the first is false then it doesn't evaluate the second, vb.net seems to ignore that so if you had and txt_name.text <> username(usernum)
and usernum was greater then 30 it would still try to evaluate the second statement and crash. Almost every other language I use would have detected that usernum > 30 and stopped there as the and was already false.

Dim loggedin As Boolean
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
usernum = 1
username(1) = "abc"
username(2) = "123"
username(3) = "efg"
loggedin = False


If txt_name.Text = username(usernum) Then
loggedin = True
Else
While usernum < 30
If loggedin = False Then
If txt_name.Text = username(usernum) Then
loggedin = True
Else
loggedin = False
End If

End If
usernum = usernum + 1

End While
If loggedin = False Then
MsgBox("Incorrect Username")
Else
MsgBox("Logged in")
End If
End If


End Sub
 

Schadenfroh

Elite Member
Mar 8, 2003
38,416
4
0
This should send you in the right direction

(this is made for VB 2005, been a while since 6)


Dim LoginFound As Boolean = False
For usernam As Integer = 0 To (username.count - 1)
If txt_name.text = username(usernam) Then
MessageBox.Show("Logged In")
loginfound = True
Exit For
End If
Next
If LoginFound <> True Then
MessageBox.show("Incorrect Username")
End If
 

joshg

Golden Member
Jul 3, 2001
1,359
0
0
That is for VB.NET and some of the syntax isn't the same for VB6 .. (for example the MsgBox class and the .Count property of an array..)

Just whipped this up in notepad and tried it out in a .vbs file and it worked perfectly. In VB6 you can add specific type declaration for variables if you want. Also you should always use something like UBound() for arrays instead of going with a hard-coded count buried in your code (similar to the way Schadenfroh posted above)




Dim username(2)

username(0)="hi"
username(1)="hello"

Dim testuser

testuser = "hello1"

Dim i
Dim LoginFound

For i = 0 to UBound(username)
If username(i)=testuser Then
LoginFound=true
Exit For
End If
Next

If Not LoginFound Then
MsgBox "Incorrect User"
else
msgbox "success!"
End If






edit: the "attach code" feature really sucks..
 

3NF

Golden Member
Feb 5, 2005
1,345
0
0
Why not use a collection with the name as the key and avoid looping
 

Evadman

Administrator Emeritus<br>Elite Member
Feb 18, 2001
30,990
5
81
'Mine:
Private Sub cmd_login_Click()
dim i
On error goto omgtherewasanerror
for i=1 to ubound(username)
if not isnull(txt_name.Text)) then
If trim(txt_name.Text) = trim(username(i)) Then
MsgBox "Logged in"
goto ExitSub
END IF
else
msgbox "Name is requried"
goto exitsub
END IF
next i
'gets here there was no match
msgbox "Incorrect user name. Please give evadman treefifdy"

ExitSub:
exit sub

omgtherewasanerror:
msgbox err.description
resume exitsub
End Sub

'I like this one better, it uses the array instead of my original one that used the hardcoadrd 1 to 30



yours fixed:
Private Sub cmd_login_Click()
usernum = 1
Do While usernum <= 30
If txt_name.Text = username(usernum) Then
MsgBox "Logged in"
goto exitsub 'this is a new line
Else
usernum = usernum + 1
End If
Loop
MsgBox "Incorrect Username"
exitsub: 'this is a new line
End Sub
 

joshg

Golden Member
Jul 3, 2001
1,359
0
0
Lots of good suggestions here, but taking what 3NF said back even further .. why are you looking up usernames in an array anyway? Why aren't you doing some kind of database query or something? Just curious.. I've never been in a situation where something that required user accounts did not have a database behind it in at least some fashion..
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |