Thursday 17 January 2013

ASP: special characters

   


ASP

One of things that newbies find difficult when trying to learn ASP is the way code is tructured and what all those special characters are there for.



In this short post we are going to see the most common special characters we can find in an ASP page:
  • &
  • '
  • _
  • :
  • .
  • <% ... %>
  • <%= ... %>
As you will see, some of the above might hide even little secrets and surprises.

The ampersand
The ampersand or "&" is used in ASP to concatenate strings.
Combining strings is easy:
<%
Dim strName
Dim strName1
Dim strName2
strName1 = "Hello"
strName2 = "world!"
strName = strName1 & " " & strName2
response.write (strName)
%>
Result:
Hello world!
That was easy.

The apostrophe
The apostrophe " ' " is used for commenting code. In any case, the code after the apostrophe is never executed.

The underscore
The underscore "_" is used to span the code on multiple lines. I must say I never use this special character in my code, but there you are. If you want your code to span on multiple lines, use the underscore:
<%
response.write ("This is a long string," &_
" and we want to keep it visible so that " &_
"it won' hide on the right side of the page!")
%>
The semicolon
This one is quite obscure: we use the semicolon ":" when we need to put multiple lines of code on just one line. Isn't it crazy? Well, if you ever need to do it, now you know how.
<%
Dim strOne
Dim strTwo
Dim strThree
strOne = "1" : strTwo = "2" : strThree = "3"
%>
Wow!

The period
We all know that with ASP we have objects because basically it is an Object Oriented Programming Language. Well, objects have methods and in order to specify an object method we use a period ".".
The object in fact is followed by its method, separated by a dot like in "myObject.Method".

The percentage
Percentage is used to delimit ASP code. This is easy:
<%
... ASP code goes here ...
%>
Equal and percentage 
This is used as a write shortcut: when we need to print something we can use <%= instead of response.write().
<%
Dim strName
strName = "Hello!"
%>
<%=strName%>
Result:
Hello!
Ok everyone. That's all for today.

2 comments:

  1. Hi Marco,

    The semicolon is new for me. Thanks.

    For a clean html source and get rid off white spaces and lines I always put the ASP code in line like: <%code%><%code%> but the semicolon works cleaner: <%code : code%>

    for example a repeat region gives normal:

    ul
    li text li

    li text li

    ul

    But when use ASP code in line with semicolon gives:

    ul
    li text li
    li text li
    ul

    ReplyDelete
    Replies
    1. Hey! That's interesting... I didn't know about that as well! Thanks.

      Delete

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.