Kraxed
Kraxed

Reputation: 350

Error in comparing strings

I have a string called str and in my event it is equal to "Administrator" when I use the code:

Dim AdminSTR As String = "Administrator"
    If str.ToString = AdminSTR Then
        Home.CurrentUser.Text = "Current User: " & UsernameTextBox.Text
        Home.CurrentUserSTR = UsernameTextBox.Text
        Home.AccessLBL.Text = " | Level: " & str
        Home.Show()
    Else
        ClientForm.Show()
    End If

It directs me to me ClientForm. While it should direct me to my Home form. Why is this so?

Thanks

Upvotes: 0

Views: 79

Answers (3)

Kraxed
Kraxed

Reputation: 350

I fixed it! I had a line of code which added a " " to the start of it to prevent it from being an empty value. Thanks to all that helped! Especially @TimSchmelter, for pointing out the cases and white-spaces! Voted up! :)

Upvotes: 0

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

Use Compare()

If String.Compare(str, AdminSTR) = 0 Then

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460018

Just guessing, maybe it's "administrator" instead, .NET is case-sensitive by default. Or you have white-spaces at the beginning/end:

If "Administrator".Equals(str.Trim(), StringComparison.OrdinalIgnoreCase) Then
    Home.CurrentUser.Text = "Current User: " & UsernameTextBox.Text
    Home.CurrentUserSTR = UsernameTextBox.Text
    Home.AccessLBL.Text = " | Level: " & str
    Home.Show()
Else
    ClientForm.Show()
End If

Upvotes: 4

Related Questions