user1890098
user1890098

Reputation: 495

value not getting assigned to variable in vbscript

if ( IsPEnabled = "TRUE" AND IsPChecked = 1) Then
    canRemove = "true"
elseif (IsPEnabled = "FALSE" AND IsPChecked = 0) Then
    canRemove = "true"
else 
    canRemove = "false"
End If

I can't seem to find any errors in the above piece of code, but whenever canRemove always remains false even if both if and else if conditions are satisfied. Note: All variables are declared and have correct values.

Upvotes: 0

Views: 140

Answers (2)

Dave
Dave

Reputation: 4356

Just to expand on this a little more, it's good practice when checking strings to force them to be either upper or lower case so that you can be certain that they match properly.

Obviously if you require to check that a string matches a specific case sensitive format, this is not an appropriate approach!

You could use:

If (UCase(IsPEnabled) = "TRUE" AND IsPChecked = 1) Then
    canRemove = "true"
ElseIf (UCase(IsPEnabled) = "FALSE" AND IsPChecked = 0) Then
    canRemove = "true"
Else 
    canRemove = "false"
End If

This way you force the change on the IsPEnabled variable purely for the comparison and don't actually change its value. Should you want to use lower case instead, you would use the following code:

If (LCase(IsPEnabled) = "true" AND IsPChecked = 1) Then
    canRemove = "true"
ElseIf (LCase(IsPEnabled) = "false" AND IsPChecked = 0) Then
    canRemove = "true"
Else 
    canRemove = "false"
End If

In general, it is also better form not to use strings for True and False values. If you had these as Boolean types instead, you wouldn't need the string comparison at all:

If (IsPEnabled AND IsPChecked = 1) Then
    canRemove = True
ElseIf (Not IsPEnabled AND IsPChecked = 0) Then
    canRemove = True
Else 
    canRemove = False
End If

Upvotes: 1

chop62
chop62

Reputation: 505

I tried this in console looking at Upper case vs lower case

this worked

if ( IsPEnabled = "true" AND IsPChecked = 1) Then
    canRemove = "true"
elseif (IsPEnabled = "false" AND IsPChecked = 0) Then
    canRemove = "true"
else 
    canRemove = "false"
End If

Upvotes: 0

Related Questions