简体   繁体   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

So what I'm trying to do is search through the array of employee login information to check if the information entered into the text box matches it.所以我要做的是搜索员工登录信息数组,检查输入到文本框中的信息是否匹配。 If so, close the current form and open the Roster Form.如果是这样,请关闭当前表格并打开名册表格。

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

This declares and array of 3 elements with indexes 0, 1, 2. Then you add values to the elements based on the input of 2 text boxes.这声明了索引为 0、1、2 的 3 个元素的数组。然后根据 2 个文本框的输入向元素添加值。 If I type Admin in TextBox1 and AdminPass in TextBox2 your array values are True , False , False .如果我在TextBox1键入 Admin 并在TextBox2中键入 AdminPass ,则您的数组值为TrueFalseFalse The third element is False because it was never assigned a value and Boolean is a value type with a default value of False .第三个元素是False ,因为它从未被赋值,并且Boolean是一个默认值为False的值类型。

Your If statement is not correct.您的If语句不正确。 An If statement with an And expects 2 statements that evaluate to Boolean .带有 And 的If语句需要 2 个评估为Boolean的语句。 The .Text in a TextBox is a String not a Boolean . TextBox中的.TextString而不是Boolean Remember your array only contains 3 elements that are True , False , False .请记住,您的数组仅包含 3 个元素,它们是TrueFalseFalse Doesn't make any sense.没有任何意义。

I am guessing you want to compare what the user types in to a list of accepted user names and passwords.我猜您想将用户输入的内容与接受的用户名和密码列表进行比较。 I made a Class to represent this.我做了一个Class来代表这个。 I added a parameterized Sub New so it would be easy to add new employees.我添加了一个参数化的Sub New ,以便添加新员工。 I overrode .ToString so we would have an easy way to compare.我覆盖了.ToString ,所以我们有一个简单的比较方法。

In the Button.Click you create a list of employees and add the employees to the list.Button.Click中,您可以创建员工列表并将员工添加到列表中。 Now you can loop through the list and when you find a match show the next form.现在您可以遍历列表,并在找到匹配项时显示下一个表单。 If the loop completes without a match the failure message box is shown.如果循环完成但没有匹配,则会显示失败消息框。

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

This is only one approach.这只是一种方法。 There are many ways to accomplish this.有很多方法可以做到这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试将该数组写入HTML,但它不断出现未定义的状态 - I am trying to write the array to HTML but it keeps coming up as undefined 我正在尝试在 Numpy arrays 中运行代码。但是属性错误不断出现 - I am trying to run code in Numpy arrays .. But an attribute error keeps coming up 我正在尝试使用字符串的大小为2d数组创建一个正方形,但是它是矩形的 - I'm trying to create a square for 2d array using the size of a string, but it's coming out rectangular 我正在尝试通过每个循环将值添加到数组中 - I'm trying to add values into an array through a for each loop 我正在尝试创建和调用使用多个语句的PHP函数 - I'm trying to create and call a PHP function that uses multiple statements 当我试图在函数中返回此数组时,为什么会出现错误“数组初始值设定项必须是初始化列表”? - Why do I get the error “Array initializer must be an initializer list” when I'm trying to return this array in a function? 我正在尝试使函数对数组中的int进行排序,但是运行此代码时出现错误。 我的错误在哪里? - I'm trying to make a function to sort ints in an array but i get an error when this code is being ran. Where is my error? 我试图了解array_merge()函数 - I'm trying to understand array_merge() function (VHDL)尝试从阵列输出时收到错误 - (VHDL) I'm Receiving an Error When Trying to Output from an Array 在 Ruby on Rails 应用程序中,我试图在数组中的散列中循环遍历数组。 为什么我收到“语法错误”消息? - In a Ruby on Rails app, I'm trying to loop through an array within a hash within an array. Why am I getting "syntax error" message?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM