No More Variants

VBScript is not a strongly typed language. In VBScript, you cannot declare the type of a variable, which results in all variables being created as variants. Variants are not supported by the .NET Framework. The .NET Framework equivalent to a variant is the Object type.

Consider the following script. In this script, a variable is declared, a value is assigned to it, and its value is displayed:

 <% Dim strMessage strMessage = "Hello!" Response.Write( strMessage ) %> 

Because the type of the strMessage variable is not explicitly declared, it is treated as an Object . When the string "Hello!" is assigned to the variable, the variable is automatically converted to a String data type.

Automatically converting variables to the appropriate data type is convenient , but it has a negative impact on performance. For performance reasons, the variable type of strMessage should be explicitly declared like this:

 <% Dim strMessage As String strMessage = "Hello!" Response.Write( strMessage ) %> 

The process of automatically converting a variable to the right type is called late binding . You can avoid late binding in your code by using the Option Strict statement. You enable Option Strict by adding the following page directive to the top of an ASP.NET page:

 <%@ Strict="True" %> 

When you enable Option Strict , Option Explicit is also automatically enabled.

Alternatively, you can enable Option Strict for every ASP.NET page in the machine.config file. To do so, add the attribute strict="true" to the < compilation > section.

ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: