how do i make a vbscript data type subtype LONG to get it to be 2,147,483,647?

我怕爱的太早我们不能终老 提交于 2021-02-08 06:21:54

问题


I have a web page that displays 10 images at a time from a directory. In the directory I have now 55,000 images. Once zz below hits 32,767 it stops. How do I make ZZ into a subtype LONG to get it to be 2,147,483,647 (the code below is not accurate, just quickly done to show you the loop I am achieving)

pp = Request("pp")  ' pp could be at 40000

filecount = 0 

dim zz

For Each file in filecoll

    zz = zz + 1

    If ZZ > PP then 

        response.write  'show image here 

    end if

Next

回答1:


The actual problem got nothing to do with long type - at least not directly. Your problem is that pp is a string while it should be numeric. To solve your problem just convert it to long:

pp = CLng(Request("pp"))

Then the comparison will work just fine.

In short, the problem is when comparing two variables - as explained in this great blog post by Eric Lippert when one variable contains a string value and one variable contains numeric value and you compare them, any string is greater than any number - so no matter what is the value of zz it means that pp will always be greater.

Now for a more detailed explanation. (Using some help from Nilpo)

In VBScript, all variables are actually of the type Variant. They are loosely typed, meaning that they can hold any type of data at any point and the type of value they contain can be changed.

VBScript's comparison operators (<,>,<>,=) are used to make numeric comparisons. Since a string is not a numeric data type, these comparison operators will never return a usable result when comparing a string to a number. That does not mean that a string cannot contain a numeric value. It could contain a string of numbers or letters that represent a numeric value such as "4.56", "4.5e3", or "fifteen". In cases like this, VBScript provides a series of conversion functions that instruct the VBS interpreter to treat a value as a specific type. It also provides the IsNumeric function that returns true or false to indicate whether a string value can be treated as a number.

Considering all of this, the proper way of doing this would be:

pp = Request("pp")

filecount = 0

Dim zz = 0

For Each file In filecoll
    zz = zz + 1
    '...
Next

If IsNumeric("pp") Then
    If zz > CLng(pp) Then
        Response.Write  'show image here
    End If
Else
    ' pp is not a number, perhaps it is empty or contains bad characters
End If

Other things to keep in mind:

  1. A Variant containing a single value is either a String or a numeric data type. That means that data types such as Currency or Date and booleans are inherently numbers.
  2. Variants may also contain references to objects and arrays. Like Strings, these cannot be used in numeric comparisons.
  3. Null, Empty, and Nothing are also valid values to consider that indicate the state of a variable. These are used when a variable is not assigned a real value. Being non-numeric, they will not work in numeric comparisons.
  4. VBScript provides specific functions and operators for comparing non-numeric values.
  5. Because VBScript does not use strict data types, data width is not a concern when comparing numbers with different precision. For the sake of the comparison, both operands will be compared at the wider data width. (This is also true of arithmetic operations. In which case, the resulting value will have the wider data width to preserve precision.)


来源:https://stackoverflow.com/questions/11829192/how-do-i-make-a-vbscript-data-type-subtype-long-to-get-it-to-be-2-147-483-647

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!