簡體   English   中英

所以我試圖調用數組並通過 If function 傳遞登錄信息,但它一直出現錯誤

[英]So I'm trying to call the array and pas the login information through the If function but it keeps coming up with an error

所以我要做的是搜索員工登錄信息數組,檢查輸入到文本框中的信息是否匹配。 如果是這樣,請關閉當前表格並打開名冊表格。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
    Dim employee(2) As Boolean
    
    employee(0) = txtUsername.Text = "Admin" And txtPassword.Text = "AdminPass"
    employee(1) = txtUsername.Text = "User" And txtPassword.Text = "UserPass"

    'If informaiton is correct, open roster and close login      
    If txtUsername.Text And txtPassword.Text = employee(0 Or 1) Then
        Roster.Show()
        Me.Close()
    Else
        'If informaiton is incorrect, message box will open
        MessageBox.Show(String.Format("Either Username or Password is incorrect."))
    End If      
End Sub
Dim employee(2) As Boolean

這聲明了索引為 0、1、2 的 3 個元素的數組。然后根據 2 個文本框的輸入向元素添加值。 如果我在TextBox1鍵入 Admin 並在TextBox2中鍵入 AdminPass ,則您的數組值為TrueFalseFalse 第三個元素是False ,因為它從未被賦值,並且Boolean是一個默認值為False的值類型。

您的If語句不正確。 帶有 And 的If語句需要 2 個評估為Boolean的語句。 TextBox中的.TextString而不是Boolean 請記住,您的數組僅包含 3 個元素,它們是TrueFalseFalse 沒有任何意義。

我猜您想將用戶輸入的內容與接受的用戶名和密碼列表進行比較。 我做了一個Class來代表這個。 我添加了一個參數化的Sub New ,以便添加新員工。 我覆蓋了.ToString ,所以我們有一個簡單的比較方法。

Button.Click中,您可以創建員工列表並將員工添加到列表中。 現在您可以遍歷列表,並在找到匹配項時顯示下一個表單。 如果循環完成但沒有匹配,則會顯示失敗消息框。

Public Class Employee
    Public Property UserName As String
    Public Property Password As String
    Public Sub New(uName As String, pWord As String)
        UserName = uName
        Password = pWord
    End Sub
    Public Overrides Function ToString() As String
        Return $"{UserName},{Password}"
    End Function
End Class

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim lst As New List(Of Employee)
    lst.Add(New Employee("Admin", "AdminPass"))
    lst.Add(New Employee("User", "UserPass"))
    For Each emp As Employee In lst
        If $"{TextBox1.Text},{TextBox2.Text}" = emp.ToString Then
            Form2.Show()
            'Roster.Show()
            Close()
            Exit Sub
        End If
    Next
    MessageBox.Show("Either Username or Password is incorrect.")
End Sub

這只是一種方法。 有很多方法可以做到這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM