Stopping a Loop
Stopping a Loop
Stopping a Loop
Stopping a Loop
Stopping a Loop
Stopping a Loop Stopping a Loop Stopping a Loop Stopping a Loop Stopping a Loop Stopping a Loop Stopping a Loop Stopping a Loop
Stopping a Loop Stopping a Loop
Stopping a Loop
Go Back  Xtreme Visual Basic Talk > > > Stopping a Loop


Reply
 
Thread Tools Display Modes
  #1  
Old 02-24-2011, 04:12 PM
VickiT VickiT is offline
Freshman
 
Join Date: Apr 2010
Posts: 27
Default Stopping a Loop


Hey

I have a login in system that i'm creating for a game. I have this code for checking to see if the username and password are correct in the text file.

Code:
 Public Sub LoginCheck()
        For x As Integer = 0 To (lstUsername.Count)
            'the usernames have already been added to this list
            If lstUsername(x).Contains("Username: " & txtUsername.Text) Then
                'the passwords have been added to this one
                If lstPassword(x).Contains("Password: " & txtPassword.Text) Then
                    frmGame.Show()
                    Me.Hide()
                End If
            End If
        Next
    End Sub
I want to know how to make the for loop stop once the username and password have been found correctly.

Also, where would I put a message box saying that the username or password are not correct if they don't match up or they're not in there at all?

Thanks
Reply With Quote
  #2  
Old 02-24-2011, 05:10 PM
snarfblam's Avatar
snarfblamStopping a Loop snarfblam is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Apr 2005
Location: USA
Posts: 896
Default

You can add an else statement to either or both if statements, and include your message box there.
Code:
If Condition Then
    ' Place code you want to run when the condition is true here.
Else
    ' Place code to run when the condition is not met here (i.e. the message box)
End If
As for exiting the loop, in this case the easy way out is to use a Return statement, which would leave the whole function.

I hope you're planning on beefing up the program's security. As is, a partial password would match ("Password: miniature" contains "Password: mini"), though it looks as though you may have intended to compare the strings for equality (.Equals instead of .Contains). The passwords also aren't salted and hashed. Even if the program isn't meant to be terribly secure, it can still compromise passwords that people use for other purposes. If this is something you're planning on distributing, security is something you should take very seriously.
__________________
C# _VB.NET _
Reply With Quote
  #3  
Old 02-25-2011, 10:27 AM
VickiT VickiT is offline
Freshman
 
Join Date: Apr 2010
Posts: 27
Default

Ok thank you that helps!

This is the new code
... it doesn't work though!
Code:
Public Sub LoginCheck()
        For x As Integer = 0 To (lstUsername.Count)
            'the usernames have already been added to this list
            If lstUsername(x).Equals("Username: " & txtUsername.Text) Then
                'the passwords have been added to this one
                If lstPassword(x).Equals("Password: " & txtPassword.Text) Then
                    frmGame.Show()
                    Me.Hide()
                    Return
                Else : MsgBox("Wrong Username or Password")
                End If
                'this else statement doesn't work
                Else : MsgBox("Wrong Username or Password")
            End If

        Next
    End Sub
the second else statement doesn't work to check if the username is in ... because it does it in the loop ... any ideas how to change it?
Reply With Quote
  #4  
Old 02-27-2011, 02:31 PM
snarfblam's Avatar
snarfblamStopping a Loop snarfblam is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Apr 2005
Location: USA
Posts: 896
Default

First of all, you need to be a lot more specific than "it doesn't work." How did it not work? Details are important. I'm going to guess as to what you meant when you said it doesn't work and hopefully my answer will make sense to you.

You had the right idea, but every time you checked a username and it didn't match you treated it like an error. It's only an error once you've tried all of the usernames and none of them matched. I've outlined the logic below, but haven't implemented any new behavior.
Code:
    Public Sub LoginCheck()
        For x As Integer = 0 To (lstUsername.Count)
            'the usernames have already been added to this list
            If lstUsername(x).Equals("Username: " & txtUsername.Text) Then
                ' We've got a username, let's check the password.
                If lstPassword(x).Equals("Password: " & txtPassword.Text) Then
                    frmGame.Show()
                    Me.Hide()
                    Return
                Else 
                    ' Right username, wrong password. Abort now.
                End If
                'this else statement doesn't work
            Else 
                ' The username didn't match, move on to the next one.
            End If
        Next
        ' Now we've gone through all usernames, and none of them matched.
    End Sub
__________________
C# _VB.NET _
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Stopping a Loop
Stopping a Loop
Stopping a Loop Stopping a Loop
Stopping a Loop
Stopping a Loop
Stopping a Loop Stopping a Loop Stopping a Loop Stopping a Loop Stopping a Loop Stopping a Loop Stopping a Loop
Stopping a Loop
Stopping a Loop
 
Stopping a Loop
Stopping a Loop
 
-->