Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

true = -1 and false = 0 1

Status
Not open for further replies.

theEclipse

Programmer
Dec 27, 1999
1,190
US
Can someone please verify for me that this is how bad things really are?

I did this
Code:
<% response.write(cint(false) & " " & cint(true)) %>

and got: 0 -1

why do I care?

because cint("") is zero, so
if cint(request.form("count"))>-1 then
....
end if
will execute if request.form("count")[ol A][li] doesnt exist[/li][li]is greater than -1 (as it should)[/li][li]and if it is false!!!![/li][/ol]

does this seem abit annoying to anyone else?

anybody have any ideas as to how to get around it?

all I want is to test and see if a value exists and if it is a number greater than -1

Robert Carpenter
"You and I have need of the strongest spell that can be found to wake us from the evil enchantment of worldliness." - C.S. Lewis (The Weight of Glory)

robert (at) robertcarpenter (dot) net
 
Code:
if request.form("count") <> ""  'will test for no value
 if cint(request.form("count"))>-1 then 
        ....
 end if
else
  code if no value
end if

I love small animals, especially with a good brown gravy....
 
then could not you just....
Code:
 if request.form("count") <> "" and cint(request.form("count"))>-1 then
        ....
else
        ...,...
end if

Robert Carpenter
"You and I have need of the strongest spell that can be found to wake us from the evil enchantment of worldliness." - C.S. Lewis (The Weight of Glory)

robert (at) robertcarpenter (dot) net
 
yes you could. I just wrote it that way so it was easy to see the logic.

I love small animals, especially with a good brown gravy....
 
You could check Len(request.form("count")), and don't forget to check for "undefined", too.

Aren't all form variables sent as string values, or are "True" nad "False" automatically converted to boolean?

Lee
 
The reason that True is negative 1 is very simple.

Think about the binary representation of a number.... the 2s compliment for negative 1 has all of the bits high.

So False = 00000000000000000000000000000000
And True = 11111111111111111111111111111111

So your numbers are zero and negative 1.
 
Of course any non-zero number will still evaluate to True...

Perhaps Jump Not Zero (jnz) is to blame.
 
Or
Code:
If IsNumeric(Request.Form("count")) And Request.Form("count") > -1 Then
Handy function, IsNumeric.
 
thanks all....I think I like genimuse's solution the best

Robert Carpenter
"You and I have need of the strongest spell that can be found to wake us from the evil enchantment of worldliness." - C.S. Lewis (The Weight of Glory)

robert (at) robertcarpenter (dot) net
 
okay, just for the record, this is what I finally came up with:
Code:
if len(trim(request.form("count")))>0 and isnumeric(request.form("count")) and  cint(request.form("count"))>-1 then
	count = request.form("count")
elseif len(trim(request.querystring("count")))>0 and isnumeric(request.querystring("count")) and cint(request.querystring("count"))>-1 then
	count = request.querystring("count")
end if

Robert Carpenter
"You and I have need of the strongest spell that can be found to wake us from the evil enchantment of worldliness." - C.S. Lewis (The Weight of Glory)

robert (at) robertcarpenter (dot) net
 
I don't think you need the Len thing, do you? I'm pretty sure that " " or "apple" or "" will all come out as not being numeric.
 
Try this experimient
Code:
if IsNumeric(var13) then
	Response.Write "hey" & "<BR>"
else
	Response.Write "nay" & "<BR>"
end if

if len(var13) > 0 then
	Response.Write "hey" & "<BR>"
else
	Response.Write "nay" & "<BR>"
end if

_______
I love small animals, especially with a good brown gravy....
 
Danger, Danger, Potential Error Ahead :p

Alright, so on the offchance that someone does pass "Apple" to you, your going to end up with an error situation. VBScript evaluates all arguments in a conditional before deciding the overall value. That means when you hit the cInt("Apple") section (3rd part of first If) your going to have issues.

Additionally, Gen was correct in that trimming and then checking length is unnecessary since spaces will count as non-numeric.

I would suggest creating a small function to check for this situation rather then repeat the code for both if statements. Something along the lines of:
Code:
Function isPosNumeric(aStr)
   isPosNumeric = (aStr <> "") And isNumeric(aStr)
   If isPosNumeric Then isPosNumeric = (cInt(aStr) >= 0)
End Function

Now you can simply call: If isPosNumeric(Request.Form("count")) Then ...

Additionally you shouldn't run into issue with empty strings or attempting to convert words to integers.

-T

barcode_1.gif
 
an empty string tests true in IsNumeric(emptystring) and is not the same as ""

_______
I love small animals, especially with a good brown gravy....
 
tarwn-

I actually found that out earlier this morning, hence thread333-1017494 . I like the idea of the function alot tho, I am going to be using it.

Robert Carpenter
"You and I have need of the strongest spell that can be found to wake us from the evil enchantment of worldliness." - C.S. Lewis (The Weight of Glory)

robert (at) robertcarpenter (dot) net
 
pkailas said:
an empty string tests true in IsNumeric(emptystring) and is not the same as ""

Hmmm...you may want to test that theory.

I agree that an empty string tests as true in iNumeric, which is why I placed the empty string test in that line as well, with a logical AND so that both would have to be true to pass that first line.

However, "" is an empty string. If you had, perhaps, meant to say that an empty variable is not the same as "", you would have ben more correct, though the difference between an empty variable and an empty string is a non issue if you are testing for <> "".

Any time you receive back an uninitialized variable in VBScript or declare a variable without providing it with a value, you get a reference to a value of type vbEmpty. When VBScript attempts to compare two variables of differant types it will first attempt to cast them to the same type, since all variables are basically variants in VBScript. When attempting to cast a variable of type vbEmpty, there are several default values, depending on the type it is being cast to. The default for numbers is 0, the default for booleans is false, and the default for strings is a 0-length string (ie, "" or what we like to call an empty string), etc.

Therefore, by testing the value against an empty string (ie, myvar <> "") we not only make sure we aren't dealing with a string, we also make sure we aren't dealing with an empty value, since the empty value will be cast to a string during comparison. An example of the difference between these two situations can be shown using Request.QueryString:

1) Empty string from Request collection:
If your URL was: Then Request.QueryString("a") should be an empty string, but still a vbString type (not vbEmpty)
2) Empty variable form Request collection
If your URL was: Then Request.QueryString("a" should be an empty variable, but can be cast to an empty string.

To further display this point, here is an example. I added a case statement from another piece of code to convert the varType constants to text for easier reading:
Code:
<%
Dim a(5), i
a(0) = 1
a(1) = "two"
a(2) = "10101"
a(3) = "-10101"
a(4) = ""

For i = 0 to 5
	Response.Write "A(" & i & ") is: " & VariableToString(a(i)) & "<br>"
	Response.Write "&nbsp;&nbsp;&nbsp;isPosNumeric(A(" & i & ")) :: " & isPosNumeric(a(i)) & "<br>"
	Response.Write "&nbsp;&nbsp;&nbsp;A(" & i & ") = "" :: " & (a(i) = "") & "<br>"
	Response.Write "&nbsp;&nbsp;&nbsp;isNumeric(A(" & i & ")) :: " & isNumeric(a(i)) & "<br><br>"
Next

Function isPosNumeric(aStr)
   isPosNumeric = (aStr <> "") And isNumeric(aStr)
   If isPosNumeric Then isPosNumeric = (cInt(aStr) >= 0)
End Function

Public Function VariableToString(aVar)
	Select Case VarType(aVar)
		Case 0	'Empty
			VariableToString = "[Empty]"
		Case 1	'Null
			VariableToString = "[Null]"
		Case 2	'Integer
			VariableToString = "[Integer] " & aVar
		Case 3	'Long
			VariableToString = "[Long] " & aVar
		Case 4	'Single
			VariableToString = "[Single] " & aVar
		Case 5	'Double
			VariableToString = "[Double] " & aVar
		Case 6	'Currency
			VariableToString = "[Currency] " & aVar
		Case 7	'Date
			VariableToString = "[Date] #" & aVar & "#"
		Case 8	'String
			VariableToString = "[String] """ & aVar & """"
		Case 9	'Object
			VariableToString = "[Object]"
		Case 10	'Error
			VariableToString = "[Error]"
		Case 11	'Boolean
			VariableToString = "[Boolean] " & aVar
		Case 12	'Variant
			VariableToString = "[Variant]"
		Case 13	'DataObject
			VariableToString = "[DataObject]"
		Case 17	'Byte
			VariableToString = "[Byte]"
		Case Else	'8192+ - Array
			VariableToString = "[Array] Size " & UBound(aVar)
	End Select
End Function
%>

As you will notice, a variable of type vbEmpty will indeed pass the equality check against empty string. And the function will return false for that value, despite the fact that it's value can be cast to 0 during an isNumeric() test and when using functions like cInt().


-T

barcode_1.gif
 
Just an FYI, instead of using "AND" you can use "ANDALSO" which will only check the 2nd condition if the first evaluates to TRUE.

similar to "ORELSE" which will only evaluate the 2nd condition if the first evaluated to FALSE.

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Yeah, we hit that one in more detail in Eclipses other thread over here: thread333-1017494

barcode_1.gif
 
Taking into account VBScripts lack of shortcut processing, this is what I came up with:

Code:
Function isPosNumeric(aStr)
   isPosNumeric = (aStr <> "") And isNumeric(aStr)
   If isPosNumeric Then isPosNumeric = (cInt(aStr) >= 0)
End Function

''handle the number of flex fields
if isPosNumeric(request.form("count")) then
	count = cint(request.form("count"))
elseif isPosNumeric(request.queryString("count")) then
	count = cint(request.querystring("count"))
end if

The reason for using this code as opposed to the larger if statement is pointed out above by tarwn. The example that I came up with that proves this vunerability is if the variable is specified in the querystring but not given a value: ex:[tt]script.asp?count=[/tt]

Also, a note about tarwns post about empty strings....I think that
Tarwn said:
... not only make sure we aren't dealing with a string, we are ...
should read something like "not only make sure we aren't dealing with an empty string, ...." (at least thats the only way I could make sense of it all.) :)

Thanks again all.

Robert Carpenter
"You and I have need of the strongest spell that can be found to wake us from the evil enchantment of worldliness." - C.S. Lewis (The Weight of Glory)

robert (at) robertcarpenter (dot) net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top