Saturday, July 18, 2009

How do I comment blocks of ASP code?

Many people get frustrated because VBScript doesn't allow you to comment blocks of code; instead, a REM or ' is required before every line. Other languages, such as Java, JavaScript, Perl, and T-SQL allow the use of delimiters such as /* comments */ which allow you to comment single or multiple lines without much fuss. So is there a way to do this in VBScript as well?

Yes! Here are two ways of preventing a block of code from writing... the first requires two comments to make it run / not run, the second requires one line change.



<‰
Sub doNotExecuteThisCode
For Each x In Request.Form
Response.Write x & "<br>"
Next
Response.End
End Sub
‰>



Now, to execute the code again, simply comment the Sub and End Sub lines:



<‰
'Sub doNotExecuteThisCode
For Each x In Request.Form
Response.Write x & "<br>"
Next
Response.End
'End Sub
‰>



The following method uses simple logic to enter a code block.



<‰
ExecuteCodeBlock = FALSE
If ExecuteCodeBlock Then
For Each x In Request.Form
Response.Write x & "<br>"
Next
Response.End
End If
‰>



Now, to execute the code again, simply change the value of ExecuteCodeBlock to TRUE:



<‰
ExecuteCodeBlock = TRUE
If ExecuteCodeBlock Then
For Each x In Request.Form
Response.Write x & "<br>"
Next
Response.End
End If
‰>



FWIW, the latter method is a little less efficient, since the If logic is actually tested. I use it all the time, however, since it's much easier to set a debug flag on or off to affect a group of blocks of code.

Source : http://classicasp.aspfaq.com/

No comments:

Post a Comment