Declaring variables using option explicit

We use the line Option Explicit at the top of the page. The purpose of writing this line is tell the ASP engine that the variables we will be using in this page are to be declared before being used. For example if we are using a variable as v1 then before assigning any value to this variable we must declare v1 as a variable.

Dim v1 'Declaring variable v1
v1=2 'assigning value
Response.Write "Value of v1=" & v1
If we remove the line Dim v1 from above code then we will get error message as before that we have told ASP engine that we will be declaring the variable before using. So the complete code for above this would be.

<%@ Language=VBScript %>
<% Option Explicit %>

<html>
<head>
<title>(Type a title for your page here)</title>
</head>

<body >

<%
Dim v1 'Declaring variable v1
v1=2  'assigning value     	
Response.Write "Value of v1=" & v1

%>

</body>
</html>
This is how we will declare variables before using and we got the option of not using the line Option Explicit at the top of the page and use the variables without declaring. Then the question is why we need this Option Explicit and then declare the variable? The main reason is for bigger pages we may do some spelling mistakes in using variables and get undesirable results which are difficult to debug. For example let us see the code below where Options explicit is not used.

<%@ Language=VBScript %>
<html><head><title>(Type a title for your page here)</title>
</head>
<body >

<%
Dim v1 'Declaring variable v1
v1=2  'assigning value     	
Response.Write "Value of v1=" & v
%>

</body>
</html>


You can see in the above code Value of variable v1 is not printed to screen as we have wrongly used it as v ( in place of v1). Imagine such mistakes in pages where hundreds of lines are used. Here no error message is printed so difficult to indentify where the problem is.

Be the first to post comment on this article :

plus2net.com




Post your comments , suggestion , error , requirements etc here .




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer