Hi guys
I've been making a simple program using arrays and would like to add a function for a password instead of it being stored in one chunk of my button code. I'm not real good at functions yet and was wondering if someone could help me out. My code is working fine but the reason i would like it to be a function is for future development and just to teach myself more.

Public Class Form1
    'Declares 2 arrays with a maximum of 4 entries
    Dim strtotals(3) As String
    Dim strsales(3) As String
    Dim intPasswordCounter As Integer = 0


    Private Sub btnDisplayTotals_Click(sender As System.Object, e As System.EventArgs) Handles btnDisplayTotals.Click
        ' Variables for handing data
        Dim strPassword As String
        Dim strIncomes2(3) As String
        Dim sngSalary(3) As Single
        Dim sngMaximum As Single
        Dim sngCurSal As Single
        Dim strEmployee As String
        Dim strTotal(3) As String
        Const Commission As Decimal = 0.015
        Const Pay As Single = 400
        strEmployee = ""

        'Code to prompt user for password 

        strPassword = InputBox("Please Enter a Valid Password", "Password Input")
        If strPassword = "admin" Then
            intPasswordCounter = 0
            sngCurSal = 0
            For i = 0 To 3

                'Calculations

                If strsales(i) <> 0 Then
                    strTotal(i) = Str(strsales(i))
                    sngSalary(i) = Pay + Int(strsales(i)) * Commission
                Else
                    strTotal(i) = 0
                    sngSalary(i) = Pay
                End If
                strIncomes2(i) = Str(sngSalary(i))
                If sngSalary(i) > sngCurSal Then
                    sngMaximum = sngSalary(i)
                    strEmployee = lstNames.Items(i)
                End If
                sngCurSal = sngSalary(i)
                strtotals(i) = "Sales Total for " + lstNames.Items(i) + " is $" + strTotal(i) + " Salary is $" + strIncomes2(i) + vbNewLine
            Next
            lblMessage1.Text = strtotals(0) + strtotals(1) + strtotals(2) + strtotals(3) + "The Highest Salary was $" + Str(sngMaximum) + " by " + strEmployee

            'Password counter code

        Else
            intPasswordCounter += 1
            If intPasswordCounter < 3 Then
                MsgBox("Incorrect Password Please Try Again")       'Message box that appears when the user inputs an incorrect password
            Else

                MsgBox("Too many attempts. Closing...")             'Message box that appears when the user inputs an incorrect password 3 times
                End
            End If
        End If


    End Sub

    Private Sub btnSale_Click(sender As System.Object, e As System.EventArgs) Handles btnSale.Click
        'Sale(lstNames.SelectedIndex).intSales += Val(txtSales.Text)
        For i = 0 To 3
            If lstNames.SelectedIndex = i Then
                strsales(i) = txtSales.Text                   'Stores Sales data with the selected list name
                txtSales.Text = ""
            End If
        Next
    End Sub

    Private Sub btnEnd_Click(sender As System.Object, e As System.EventArgs) Handles btnEnd.Click
        End
    End Sub
End Class

Here's a simple function for you, and a way to incorporate it into your code:

Private Sub btnDisplayTotals_Click(sender As System.Object, e As System.EventArgs) Handles btnDisplayTotals.Click
    'Validate the password first thing, then exit if it returns false
    If Not ValidPassword Then Exit Sub

    ' Variables for handing data        
    Dim strIncomes2(3) As String
    Dim sngSalary(3) As Single
    Dim sngMaximum As Single
    Dim sngCurSal As Single
    Dim strEmployee As String
    Dim strTotal(3) As String
    Const Commission As Decimal = 0.015
    Const Pay As Single = 400
    strEmployee = ""
    sngCurSal = 0
    For i = 0 To 3
        'Calculations
        If strsales(i) <> 0 Then
            strTotal(i) = Str(strsales(i))
            sngSalary(i) = Pay + Int(strsales(i)) * Commission
        Else
            strTotal(i) = 0
            sngSalary(i) = Pay
        End If
        strIncomes2(i) = Str(sngSalary(i))
        If sngSalary(i) > sngCurSal Then
            sngMaximum = sngSalary(i)
            strEmployee = lstNames.Items(i)
        End If
        sngCurSal = sngSalary(i)
        strtotals(i) = "Sales Total for " + lstNames.Items(i) + " is $" + strTotal(i) + " Salary is $" + strIncomes2(i) + vbNewLine
    Next
    lblMessage1.Text = strtotals(0) + strtotals(1) + strtotals(2) + strtotals(3) + "The Highest Salary was $" + Str(sngMaximum) + " by " + strEmployee
End Sub

Private Function ValidPassword() As Boolean

    'Initialize the function to return false.  
    'The return value will only change if the correct password is entered
    ValidPassword = False

    For I = 1 To 3
        Dim strPassword As String = InputBox("Please Enter a Valid Password", "Password Input")
        If strPassword = "Admin" Then
            'Got the right password force a return with a value of True
            Return True
        End If
    Next
    'function didn't return yet, so user must have taken too many tries.
     MsgBox("Too many attempts. Closing...")
End Function

Thanks heaps

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.