Comparing Integers

Discussion in 'ASP General' started by RN1, Dec 15, 2007.

  1. RN1

    RN1 Guest

    Sometimes I find that though I am comparing 2 integers, the result
    turns out to be unexpected.

    For e.g. an ASP page encapsulates recordset paging.

    <%
    Dim iPage,iPageCounter

    iPage=Request.QueryString("Page")
    If(iPage="") Then
    iPage=1
    End If

    Dim objConn
    Set objConn=Server.CreateObject("ADODB.CONNECTION")
    objConn.Open ........

    Dim strSQL
    strSQL="SELECT * FROM MyTable"

    Dim objRS
    Set objRS=Server.CreateObject("ADODB.RECORDSET")
    objRS.PageSize=15
    objRS.Open strSQL,objConn,adOpenKeyset

    objRS.AbsolutePage=CInt(iPage)

    'display records here

    'pagination
    For iPageCounter=1 To objRS.PageCount
    If(iPageCounter=CInt(iPage)) Then
    Response.Write("[" & iPageCounter & "]")
    Else
    Response.Write("<a href='ThisPage.asp?Page=" &
    iPageCounter & "'>" & iPageCounter & "</a>")
    End If
    Next
    %>

    Assuming that there are 225 records, the For....Next loop will display
    1, 2, 3, 4.........14, 15 as page numbers.

    The If condition within the For....Next loop ensures that the page
    which the user is currently viewing isn't displayed as a hyperlink.
    For e.g. if a user is viewing page #4, then 4 is not rendered as a
    hyperlink; rather it is rendered as plain text BUT if the variable
    iPage in the If condition within the For....Next loop is not
    explicitly cast into an integer using CInt i.e. the If condition looks
    like this

    If(iPageCounter=iPage) Then

    then the page the user is currently viewing remains a hyperlink
    instead of plain text. Why?

    For e.g. when the user is viewing page #4, the value of the variable
    iPage is 4 which means that within the For...Next loop, when
    iPageCounter is 4, then the If condition evaluates to true since iPage
    is 4 & so is iPageCounter. Then why does 4 get rendered as a hyperlink
    instead of plain text?
    RN1, Dec 15, 2007
    #1
    1. Advertising

  2. "RN1" <> wrote in message
    news:...
    > Sometimes I find that though I am comparing 2 integers, the result
    > turns out to be unexpected.
    >
    > For e.g. an ASP page encapsulates recordset paging.
    >
    > <%
    > Dim iPage,iPageCounter
    >
    > iPage=Request.QueryString("Page")
    > If(iPage="") Then
    > iPage=1
    > End If
    >
    > Dim objConn
    > Set objConn=Server.CreateObject("ADODB.CONNECTION")
    > objConn.Open ........
    >
    > Dim strSQL
    > strSQL="SELECT * FROM MyTable"
    >
    > Dim objRS
    > Set objRS=Server.CreateObject("ADODB.RECORDSET")
    > objRS.PageSize=15
    > objRS.Open strSQL,objConn,adOpenKeyset
    >
    > objRS.AbsolutePage=CInt(iPage)
    >
    > 'display records here
    >
    > 'pagination
    > For iPageCounter=1 To objRS.PageCount
    > If(iPageCounter=CInt(iPage)) Then
    > Response.Write("[" & iPageCounter & "]")
    > Else
    > Response.Write("<a href='ThisPage.asp?Page=" &
    > iPageCounter & "'>" & iPageCounter & "</a>")
    > End If
    > Next
    > %>
    >
    > Assuming that there are 225 records, the For....Next loop will display
    > 1, 2, 3, 4.........14, 15 as page numbers.
    >
    > The If condition within the For....Next loop ensures that the page
    > which the user is currently viewing isn't displayed as a hyperlink.
    > For e.g. if a user is viewing page #4, then 4 is not rendered as a
    > hyperlink; rather it is rendered as plain text BUT if the variable
    > iPage in the If condition within the For....Next loop is not
    > explicitly cast into an integer using CInt i.e. the If condition looks
    > like this
    >
    > If(iPageCounter=iPage) Then
    >
    > then the page the user is currently viewing remains a hyperlink
    > instead of plain text. Why?
    >
    > For e.g. when the user is viewing page #4, the value of the variable
    > iPage is 4 which means that within the For...Next loop, when
    > iPageCounter is 4, then the If condition evaluates to true since iPage
    > is 4 & so is iPageCounter. Then why does 4 get rendered as a hyperlink
    > instead of plain text


    TBH, I can't explain or reproduce your problem. If page is specified on the
    query string then the type of iPage is a string. However when making a
    comparison between an integer and a string VBScript will coerce the string
    to an integer and the comparison should succeed.

    That said since iPage is expected to be an integer and has prefix notation
    to that effect it would be best to ensure that it is on input:-

    Dim iPage: iPage = CInt(Request.QueryString("Page"))
    If iPage = 0 Then iPage = 1

    No need for any further CInt calls.


    --
    Anthony Jones - MVP ASP/ASP.NET
    Anthony Jones, Dec 15, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nag
    Replies:
    3
    Views:
    532
  2. Elijah Bailey

    comparing long integers

    Elijah Bailey, Jan 22, 2004, in forum: C++
    Replies:
    12
    Views:
    603
    Dan Pop
    Jan 23, 2004
  3. Nicholas

    Comparing char* with integers and characters

    Nicholas, Sep 9, 2003, in forum: C Programming
    Replies:
    13
    Views:
    607
    Nicholas
    Sep 9, 2003
  4. Elijah Bailey

    comparing long integers

    Elijah Bailey, Jan 22, 2004, in forum: C Programming
    Replies:
    11
    Views:
    632
    Dan Pop
    Jan 23, 2004
  5. Replies:
    5
    Views:
    446
    Roy Smith
    May 20, 2004
Loading...

Share This Page