Dimming A Variable More Than Once

A

Arpan De

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
 
C

Cowboy \(Gregory A. Beamer\)

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.


Arpan De said:
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
 
J

Jon Mundsack

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 said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top