[英]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 ,则您的数组值为True
、 False
、 False
。 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
中的.Text
是String
而不是Boolean
。 Remember your array only contains 3 elements that are True
, False
, False
.请记住,您的数组仅包含 3 个元素,它们是True
、 False
、 False
。 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.