String Visual Basic字符串等于运算符不工作 If(UsersTableAdapter.FillByUsername(Me.DatabaseDataSet1.users,username)0)则 type=DatabaseDataSet1.users.First.UserType MsgBox(类型) 如果type=“Staff”,则 MsgBox(“您是员工”) ElseIf type=“学生”则 MsgBox(“您是学生”) 其他的 MsgBox(“其他东西”) 如果结束 如果结束
我的代码首先从数据库中获取用户的String Visual Basic字符串等于运算符不工作 If(UsersTableAdapter.FillByUsername(Me.DatabaseDataSet1.users,username)0)则 type=DatabaseDataSet1.users.First.UserType MsgBox(类型) 如果type=“Staff”,则 MsgBox(“您是员工”) ElseIf type=“学生”则 MsgBox(“您是学生”) 其他的 MsgBox(“其他东西”) 如果结束 如果结束,string,visual-studio,visual-studio-2013,comparison,basic,String,Visual Studio,Visual Studio 2013,Comparison,Basic,我的代码首先从数据库中获取用户的UserType。然后将类型的值与职员和学生进行比较。我在输出type中添加了一个消息框,并确认type已正确从数据库中提取 为什么if在比较中不返回true 我也尝试过使用.equals()和.toString()函数,但仍然没有成功。我支持@Steve,知道最可能的问题是什么。您可能想尝试使用ToLower()和Trim() 请注意Steve在以下代码中提到的两种情况(大小写和隐藏空格): If (UsersTableAdapter.FillByUsernam
UserType
。然后将类型
的值与职员
和学生
进行比较。我在输出type
中添加了一个消息框,并确认type
已正确从数据库中提取
为什么if
在比较中不返回true
我也尝试过使用
.equals()
和.toString()
函数,但仍然没有成功。我支持@Steve,知道最可能的问题是什么。您可能想尝试使用ToLower()
和Trim()
请注意Steve在以下代码中提到的两种情况(大小写和隐藏空格):
If (UsersTableAdapter.FillByUsername(Me.DatabaseDataSet1.users, username) <> 0) Then
type = DatabaseDataSet1.users.First.UserType
MsgBox(type)
If type = "Staff" Then
MsgBox("You are staff")
ElseIf type = "Student" Then
MsgBox("You are student")
Else
MsgBox("Something else")
End If
End If
同样的情况?“Staff”“Staff”还检查类型变量末尾是否有隐藏空格(“Staff”的MsgBox(type.Length)应为5)。开始使用枚举而不是字符串的时间到了。谢谢,yeh,在向db写入数据时,出于某种原因,有一堆空格出现了!
Dim Type = "staff "
MsgBox(Type)
If Type.ToLower().Trim() = "Staff".ToLower() Then
MsgBox("You are staff")
ElseIf Type = "Student" Then
MsgBox("You are student")
Else
MsgBox("Something else")
End If