VB - Array
In this tutorial I will explain how you can use arrays to make a simple login database.
consisting of a collection of elements ( data structurevalues or variables), each identified by at least one index. An array is stored so that the position of each element can be computed from its index tuple by a mathematical formula.
An array data structure or simply array is aLet's start by creating a new windows form application project. Call it Array.
In this application, our array will contain variables. When you close the program, all variables will be lost.
Add 2 labels, 2 textboxes and 1 button to your form:
Double click your form, and declare the following variable as a public (You will be able to change certain usernames/passwords in other forms) :
Public strPersoneel(30, 1) As String
Strpersoneel(30 stands for the ammount of usernames that will be able to login +1 because an array starts at 0, 1 stands for the password dimension corresponding the username)
Right now, we have an empty string array. To be able to log in to your program, we will need to manually add some usernames and passwords.
We will do this in the form1_load sub:
strPersoneel(0, 0) = "Username"
strPersoneel(0, 1) = "Password"
Go back to the design of your form and double click the button.
Add the following codes to the button1_click sub:
Dim i As Integer
For i = 0 To strPersoneel.GetUpperBound(i)
If txtUsername.Text = strPersoneel(i, 0) And txtPassword.Text = strPersoneel(i, 1) Then
Me.Hide()
' show next form.
Exit Sub
Else
If i = strPersoneel.GetUpperBound(0) Then
MessageBox.Show("Username or password is incorrect !", "Login error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End If
Next
This code will go through all the variables that are present in the strPersoneel string array and check if the username and password are correct. If no corresponding username or password is found it will show an error message saying the username or password is incorrect. If the username and password are correct it will hide the form and exit the sub. If you do not add the Exit Sub line, it will keep going through all variables in the array.
You have succesfully created a login application using an array. This method of logging in is more usefull than using If this = that ...
Download the source codes:
Topic: VB - Array
No comments found.