VBS numeric const doesn't work in> = comparison for loop

I'm having a weird problem (IMHO) that might just highlight my ignorance when it comes to VBS and other programming languages. Here's what's on top of my .VBS file.

Option Explicit
Const MAXROWSFOREXCEL2010 = 1048576

      

Then I get the following conditional check in a loop:

If numLines >= MAXROWSFOREXCEL2010 Then
   wscript.echo "Inside the numLines If Then Statement"
   wscript.echo "numLines = " & numLines & " >= " & MAXROWSFOREXCEL2010

      

As an example, let's say numLines is 107563, which is clearly less than 1 million plus the value assigned to MAXROWSFOREXCEL2010 as the global const above.

For some reason, the IF statement is executed even if numLines is clearly less than const.

However, if I remove the use of the constant in the comparison and just put the hard coded value of MAXROWSFOREXCEL2010 in a loop, like so:

If numLines >= 1048576 Then
   wscript.echo "Inside the numLines If Then Statement"
   wscript.echo "numLines = " & numLines & " >= " & MAXROWSFOREXCEL2010

      

Then the IF statement is NOT entered incorrectly.

Can someone please tell me why this is so? Should I somehow declare const as a concrete data type? Is there some kind of const truncation?

+3


source to share


2 answers


I would be inclined to suggest , the problem is numLines

not numeric, and not a value problem Const

, in which case this question answers the problem.

From A: VBScript implicit conversion in IF expression other than variable in literature? @ cheran-shunmugavel
The documented behavior is that when comparing, the number is always less than the string. This is stated in the documentation for Comparison Operators . To paraphrase the table at the bottom of the page:

If one expression is numeric and the other is a string , then the numeric expression is less than the string expression.



With that in mind, you just need to make sure the variable is numLines

explicitly a numeric value using an explicit cast.

'Explicitly cast variable to Long
numLines = CLng(numLines)

      

+2


source


Ok, I believe what's going on is a quirk of the way VBS tries to figure out data types. I also believe the variable numLines

is evaluated as String

.

You can do Wscript.Echo TypeName(numLines)

to check this. I can replicate your problem when I set numLines

equal "107563"

instead of 107563

.

If VBS specifies "boxed", in the case of your const MAXROWSFOREXCEL2010

, the engine will try to perform the comparison differently than it would not. In this case, since numLines

is a string and the comparison is against a boxed variable, it tries to perform a (bitwise) string comparison. If numLines

is a string and the comparison is against an explicit Integer

comparison, the comparison is performed as a (bitwise) integer comparison.

If there numLines

was Integer

, this problem does not arise.



Here are some more examples:

strOne = "1"
intOne = 1
If"1" = 1 Then WScript.Echo"true"Else WScript.Echo"false"
If"1" = intOne Then WScript.Echo"true"Else wscript.Echo"false"
If strOne = 1 Then WScript.Echo"true"Else wscript.Echo"false"
If strOne = intOne Then WScript.Echo"true"Else wscript.Echo"false"

      

Output

true
true
true
false

      

0


source







All Articles