Changes from Classic ASP

IOTA^_^    

Sams Teach Yourself ASP.NET in 21 Days, Second Edition
By Chris Payne
Table of Contents
Appendix B.  Common ASP.NET Mistakes


When moving from classic ASP and VBScript to ASP.NET and VB.NET, developers will often run into syntactical errors or other problems due to the migration. The following sections describe these errors and their solutions.

Problems with VBScript

Error: Wend is no longer supported; use End While instead.

Description: You've tried to close a while statement with the keyword wend.

Solution: In VB.NET, the wend keyword is not supported. Simply use End While instead.

Error: The syntax <lower bound> To <upper bound> is no longer supported for specifying array bounds.

Description: Your array is trying to declare a fixed size.

Solution: In VBScript, you are able to use dim MyArray(0 to 5) to declare a fixed-size array. This is not supported in VB.NET.

Error: The name 'myArray' is not declared. where myArray is an array variable.

Description: You haven't yet declared the array variable.

Solution: You are trying to declare an array using the ReDim keyword. In VB.NET, you must declare an array using the Dim keyword before using ReDim, unlike VBScript.

Error: A value of type 'type' cannot be converted to 'object'.

Description: This typically means that you are trying to assign a property to an object without specifying the property name.

Solution: In VBScript, objects supported default properties. In other words, you weren't required to specify the name of a property to set it. For example, given a Label control, the following would be possible in VBScript:

 dim Label as Label Label = "Hello World" 

This would set the Text property to the string "Hello World". In VB.NET, default properties are no longer supported unless they take parameters. You must instead use the following:

 Label.Text = "Hello World" 

Problem: Let and Set are no longer supported on assignment statements.

Description: You are using the Let or Set keyword to assign something to an object.

Solution: These keywords are not supported in VB.NET. Simply remove them and your code should work.

Problem: The name 'N' is not declared. even though you have declared it.

Description: The declaration for a variable in one part of your page is not accessible by another part of your page.

Solution: In VBScript, variables declared within blocks (any set of statements terminated with End, Next, or Loop) are viewable outside the block. For example, the following code will write 11 to the browser:

 dim I as integer For I = 1 To 10    Dim N As Double    N = N + I Next Response.write(N) 

In VB.NET, the variable N in the preceding code has block scope, meaning it is not accessible outside the code block (the for loop, in this case). Thus, this code will produce an error. Instead, declare your variable outside the loop.

Problem: Optional parameters must always specify a default value.

Description: You've declared a function with the optional keyword, specifying that a parameter is optional.

Solution: In VB.NET, optional parameters must specify a default value. For instance, change the following VBScript code:

 sub MySub(Optional MyParam as String) 

to

 sub MySub(Optional MyParam as String = "HI") 

Also, the IsMissing function, used to detect whether an optional parameter was specified in VBScript, is not supported in VB.NET.

Problem: Argument lists in all call statements must now be enclosed in parentheses.

Description: You tried to call a function or sub without using parentheses. For example:

 Response.Write "Hello World" 

Solution: In VB.NET, all function and sub calls must enclose any parameters in parentheses, whether or not they return values. Instead of the preceding code, use the following:

 Response.Write("Hello World") 

Problem: Type-declaration character & does not match declared data type type.

Description: You are trying to use the ampersand to concatenate strings.

Solution: In VBScript, it was possible to concatenate strings using the ampersand, without any spaces between variable names. For example, the following code in VBScript would write HI there everyone to the browser:

 dim a as string = "HI " dim b as string = "there " dim c as string = "everyone" Response.Write(a&b&c) 

This will cause an error in ASP.NET. Instead, use

 Response.Write(a & b & c) 

Problems with Classic ASP

Problem: Syntax Error or Expected variable, constant, Enum, Type, or procedural declaration.

Description: Method and global variable declarations don't work properly. You are trying to declare a method or variable within a code render block. For example:

 <%    dim I as Integer    sub HelloWorld       ...    end sub %> 

Solution: In VB.NET, all method and global variable declarations must be contained within code declaration blocks (within <script> tags). Also, any non-assignment statements must be placed within another method, such as Page_Load.

Problem: Request doesn't return what you expect it to.

Description: You are using the Request object to return data from the HTTP request; for example, Request.Form or Request.QueryString.

Solution: In classic ASP, the Request object would return strings containing the entire collection of variables. For example, given the URL http://localhost/test/Test.asp?values=45&values=600 and the following code:

 Response.Write(Request.Querystring(values) 

classic ASP would display a single string, 45, 600, in the browser. In ASP.NET, the Request object returns arrays of strings instead of one concatenated string. For example, with the earlier URL and the code

 Response.Write(Request.Querystring(values)(0) 

ASP.NET would display 45, and the following:

 Response.Write(Request.Querystring(values)(1) 

would display 600. This functionality is the same for Request, Request.Querystring, and Request.Form.


    IOTA^_^    
    Top


    Sams Teach Yourself ASP. NET in 21 Days
    Sams Teach Yourself ASP.NET in 21 Days (2nd Edition)
    ISBN: 0672324458
    EAN: 2147483647
    Year: 2003
    Pages: 307
    Authors: Chris Payne

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net