【问题标题】:Visual Basic string equals operator not workingVisual Basic 字符串等于运算符不起作用
【发布时间】:2014-07-10 21:26:37
【问题描述】:
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

我的代码首先从数据库中获取用户的UserType。然后它将type 的值与StaffStudent 进行比较。我添加了一个消息框来输出type,并确认type 已从数据库中正确提取。

为什么if 在比较中没有返回true

我也尝试过使用.equals().toString() 函数,但仍然没有成功。

【问题讨论】:

  • 同样的情况? "Staff" "staff" 还检查类型变量末尾是否有隐藏空格(对于 "Staff",MsgBox(type.Length) 应该为 5)
  • 是时候开始使用枚举而不是字符串了。

标签: string visual-studio visual-studio-2013 comparison basic


【解决方案1】:

我同意@Steve 最可能的问题是什么。您可能想尝试使用ToLower()Trim()

请注意史蒂夫在以下代码中提到的两种情况(大小写和隐藏空格):

    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

【讨论】:

  • 谢谢,是的,由于某种原因,当写入数据库时​​,一堆空格进入了那里!
猜你喜欢
  • 2016-05-23
  • 2014-10-09
  • 2019-07-27
  • 2019-03-19
  • 1970-01-01
  • 2021-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多