Dimming A Variable More Than Once

Discussion in 'ASP General' started by Arpan De, Aug 25, 2003.

  1. Arpan De

    Arpan De Guest

    Suppose I have the following ASP code:

    <%
    Dim strName
    .....................
    .....................
    .....................
    .....................
    .....................
    .....................
    Dim strName
    %>

    The above code will throw an error saying Name redefined pointing to the second Dim strName. That's OK but why isn't the same
    error thrown in the following case?

    <%
    Dim objConn
    Set objConn=CreateObject("ADODB.CONNECTION")
    'ConnectionString

    Dim strSQL
    strSQL="SELECT....FROM....WHERE....ORDER BY...."

    Dim objRS
    Set objRS=CreateObject("ADODB.RECORDSET")
    objRS.Open strSQL,objConn

    Do Until(objRS.EOF)
    Dim strName
    strName=objRS("Name")
    ..........................
    ..........................
    ..........................
    ..........................
    ..........................
    objRS.MoveNext
    Loop
    %>

    In the 2nd code snippet, the variable strName is Dim(med) for the 1st DB record. When the cursor comes to the 2nd record, again
    strName is Dim(med). strName is yet again Dim(med) when the cursor comes to the 3rd record so on & so forth. So why isn't the Name
    redefined error being thrown here? Isn't the variable strName getting Dim(med) here more than once in the same way as the variable
    strName is Dim(med) more than once in the 1st code snippet? Is the variable strName destroyed & recreated after each record in the
    2nd code snippet?

    Thanks,

    Arpan


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.502 / Virus Database: 300 - Release Date: 18/07/2003
    Arpan De, Aug 25, 2003
    #1
    1. Advertising

  2. Think of it, conceptually, like this:

    Do Until(objRS.EOF)
    PrintName(objRS)
    objRS.MoveNext
    Loop

    Private Function PrintName(objRS)
    Dim strName
    stName = objRS("name")

    Response.Write(strName)

    End Function

    When you call each iteration of the loop, it is as if you were calling a
    function and declaring the variable again.

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA
    Author: ADO.NET and XML: ASP.NET on the Edge

    ****************************************************************************
    ****
    Think Outside the Box!
    ****************************************************************************
    ****
    "Arpan De" <> wrote in message
    news:...
    > Suppose I have the following ASP code:
    >
    > <%
    > Dim strName
    > .....................
    > .....................
    > .....................
    > .....................
    > .....................
    > .....................
    > Dim strName
    > %>
    >
    > The above code will throw an error saying Name redefined pointing to the

    second Dim strName. That's OK but why isn't the same
    > error thrown in the following case?
    >
    > <%
    > Dim objConn
    > Set objConn=CreateObject("ADODB.CONNECTION")
    > 'ConnectionString
    >
    > Dim strSQL
    > strSQL="SELECT....FROM....WHERE....ORDER BY...."
    >
    > Dim objRS
    > Set objRS=CreateObject("ADODB.RECORDSET")
    > objRS.Open strSQL,objConn
    >
    > Do Until(objRS.EOF)
    > Dim strName
    > strName=objRS("Name")
    > ..........................
    > ..........................
    > ..........................
    > ..........................
    > ..........................
    > objRS.MoveNext
    > Loop
    > %>
    >
    > In the 2nd code snippet, the variable strName is Dim(med) for the 1st DB

    record. When the cursor comes to the 2nd record, again
    > strName is Dim(med). strName is yet again Dim(med) when the cursor comes

    to the 3rd record so on & so forth. So why isn't the Name
    > redefined error being thrown here? Isn't the variable strName getting

    Dim(med) here more than once in the same way as the variable
    > strName is Dim(med) more than once in the 1st code snippet? Is the

    variable strName destroyed & recreated after each record in the
    > 2nd code snippet?
    >
    > Thanks,
    >
    > Arpan
    >
    >
    > ---
    > Outgoing mail is certified Virus Free.
    > Checked by AVG anti-virus system (http://www.grisoft.com).
    > Version: 6.0.502 / Virus Database: 300 - Release Date: 18/07/2003
    >
    >
    Cowboy \(Gregory A. Beamer\), Aug 25, 2003
    #2
    1. Advertising

  3. Arpan De

    Jon Mundsack Guest

    Dim is not a run time statement, it is compile time. ASP essentially treats
    all Dim statements as if they are at the top of their context (procedure vs.
    module). So, in your example, this is what ASP "sees":

    <%
    Dim objConn
    Dim strSQL
    Dim objRS
    Dim strName

    Set objConn=CreateObject("ADODB.CONNECTION")
    'ConnectionString

    strSQL="SELECT....FROM....WHERE....ORDER BY...."

    Set objRS=CreateObject("ADODB.RECORDSET")
    objRS.Open strSQL,objConn

    Do Until(objRS.EOF)
    strName=objRS("Name")
    ..........................
    ..........................
    ..........................
    ..........................
    ..........................
    objRS.MoveNext
    Loop
    %>

    HTH-Jon



    "Arpan De" <> wrote in message
    news:...
    > Suppose I have the following ASP code:
    >
    > <%
    > Dim strName
    > .....................
    > .....................
    > .....................
    > .....................
    > .....................
    > .....................
    > Dim strName
    > %>
    >
    > The above code will throw an error saying Name redefined pointing to the

    second Dim strName. That's OK but why isn't the same
    > error thrown in the following case?
    >
    > <%
    > Dim objConn
    > Set objConn=CreateObject("ADODB.CONNECTION")
    > 'ConnectionString
    >
    > Dim strSQL
    > strSQL="SELECT....FROM....WHERE....ORDER BY...."
    >
    > Dim objRS
    > Set objRS=CreateObject("ADODB.RECORDSET")
    > objRS.Open strSQL,objConn
    >
    > Do Until(objRS.EOF)
    > Dim strName
    > strName=objRS("Name")
    > ..........................
    > ..........................
    > ..........................
    > ..........................
    > ..........................
    > objRS.MoveNext
    > Loop
    > %>
    >
    > In the 2nd code snippet, the variable strName is Dim(med) for the 1st DB

    record. When the cursor comes to the 2nd record, again
    > strName is Dim(med). strName is yet again Dim(med) when the cursor comes

    to the 3rd record so on & so forth. So why isn't the Name
    > redefined error being thrown here? Isn't the variable strName getting

    Dim(med) here more than once in the same way as the variable
    > strName is Dim(med) more than once in the 1st code snippet? Is the

    variable strName destroyed & recreated after each record in the
    > 2nd code snippet?
    >
    > Thanks,
    >
    > Arpan
    >
    >
    > ---
    > Outgoing mail is certified Virus Free.
    > Checked by AVG anti-virus system (http://www.grisoft.com).
    > Version: 6.0.502 / Virus Database: 300 - Release Date: 18/07/2003
    >
    >
    Jon Mundsack, Aug 25, 2003
    #3
    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. Paul Aspinall
    Replies:
    1
    Views:
    372
    Terry Burns
    Feb 5, 2006
  2. TheKeith
    Replies:
    3
    Views:
    840
    TheKeith
    Dec 13, 2003
  3. Gancy
    Replies:
    4
    Views:
    160
    Rasto Levrinc
    Feb 3, 2005
  4. Steven D'Aprano
    Replies:
    0
    Views:
    65
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    62
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page