-
Mar 20th, 2012, 03:12 PM
#1
Thread Starter
New Member
Problem with conditional statements
Code:
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim alltext() As String = System.IO.File.ReadAllLines("C:\Users\Parent\Documents\Visual Studio 2008\Projects\Members.txt")
Dim lookfor As String = txtLogin.Text & ","
If txtLogin.Text = "Admin" And txtPassword.Text = "DWADMIN" Then
MsgBox("You are now successfuly logged in")
Form2.Show()
Me.Hide()
'generic admin login for all staff
End If
For Each line As String In Filter(alltext, lookfor)
If line.StartsWith(txtLogin.Text) Then
MsgBox("You are now successfully logged in")
Form3.Show()
Me.Hide()
End If
Next
If txtLogin.Text = "" Or txtPassword.Text = "" Then
MsgBox("Please enter Username/Password")
End If
Right basically I'm having trouble using if and elseif statements. I cant use these three small pieces of code because I constantly get 'Else must be preceded by an If or ElseIf' but there is nowhere for me to put these without these erroring?
-
Mar 20th, 2012, 05:03 PM
#2
Re: Problem with conditional statements
I'm not fully clear on what you are trying to do, but something like this seems more likely:
Code:
If txtLogin.Text = "" OrElse txtPassword.Text = "" Then
MsgBox("Please enter Username/Password")
ElseIf txtLogin.Text = "Admin" AndAlso txtPassword.Text = "DWADMIN" Then
MsgBox("You are now successfuly logged in")
Form2.Show()
Me.Hide()
'generic admin login for all staff
Else
For Each line As String In Filter(alltext, lookfor)
If line.StartsWith(txtLogin.Text) Then
MsgBox("You are now successfully logged in")
Form3.Show()
Me.Hide()
End If
Next
End If
I'm taking a big guess with where that For Loop went. I figured that you wanted to require something in both textboxes, and if either was empty, scold the user. That's the If part. Next, if there were specific things in the textboxes, check to see whether they are the Admin credentials. That's the ElseIf part. Finally, if there was something in the textbox, and it wasn't the admin credentials, use the For loop to check the file. That's the Else part.
My usual boring signature: Nothing
-
Mar 20th, 2012, 09:38 PM
#3
Addicted Member
Re: Problem with conditional statements
Please show the code with the "elseif" statements that is causing the error, we can't help if we can't see the broken part.
-
Mar 20th, 2012, 10:09 PM
#4
Re: Problem with conditional statements
So many people seem to either forget or ignore that programming doesn't exist in a vacuum. Code is an implementation of a solution to a real-world problem. It is AN IMPLEMENTATION OF A SOLUTION, it is NOT the solution itself. Looking at Shaggy's code from post #2, that can easily be described in simple English:
If the login and password text boxes are empty then notify the user of the error, otherwise if the login and password text boxes contain "Admin" and "DWADMIN" respectively then notify the user that they're logged in and proceed, otherwise loop through a list of user names and if the line starts with the contents of the contents of the login text box then notify the user that they're logged in and proceed.
That is the solution. The reason that you and so many other people have problems is because you're trying to implement the solution without a clear understanding of what the solution is that you're trying to implement. There's very little chance of the code you write doing what you want it to do if you don't know what it is that you want it to do.
You should be doing things the other way around. You should be picking up a pen and paper (remember those?) and writing out the solution, something like what I provided above. From that, you can then write pseudo-code that implements that solution. I'm assuming that beginner courses still teach pseudo-code and everyone still ignores it. Once you have the pseudo-code you can actually run through it manually with some inputs and outputs to make sure that it works in all situations. Only then should you approach the keyboard and start to write actual VB code. At that point you will have a very clear understanding of what the code has to do so, while there may still be issues, you won;t have large chunks of code in completely the wrong order.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|