Language Changes

Snoops

   

 
Migrating to .NET: A Pragmatic Path to Visual Basic .NET, Visual C++ .NET, and ASP.NET
By Dhananjay  Katre, Prashant  Halari, Narayana  Rao  Surapaneni, Manu  Gupta, Meghana  Deshpande

Table of Contents
Chapter 7.   Migrating to ASP.NET II


The language changes discussed here are with reference to Visual Basic .NET because this is the preferred language for migration.

Variable Declaration

In ASP, the Option Explicit keywords were available but not enforced as the default. In Visual Basic .NET this has changed. Option Explicit is now the default, so all variables need to be declared.

Because VBScript is no longer supported, we can make use of data types, which are made available through Visual Basic. Thus, variable declarations will now look like:

 Dim intPtr as integer  Dim strName as String 

The declarations have to be made explicitly with reference to the data types because the explicit option is set to True. This can be set as an attribute of the Page directive as follows :

 <%Page Language="vb" Explicit="true"%> 

It can also be set in the compilation section of the web.config :

 <compilation defaultLanguage="vb" explicit="true"/> 

The default data type is now object instead of variant and as a result, variables declared without specifying data types are treated as objects. Type conversions have to be carried out explicitly. The following piece of code in ASP shows how type conversions are loosely carried out:

 graphics/icon01.gif Dim Str  Str = 5  Response.write Str  Str ="Hello"  Response.write Str 

This code would require the following changes before running successfully under .NET:

 graphics/icon01.gif Dim str as object  str = 5  Response.Write(str.ToString())  str = "Hello"  Response.Write(str.ToString()) 

Because the default data type is object , we can access methods like ToString provided by object .

If the Strict option is set to off, as follows, either at the page or application level in the web.config , the preceding code ASP code would be fine:

 <%@ Page Language="vb" Strict="off" %> 

or

 <compilation defaultLanguage="vb" Strict="off" /> 

Property Declaration

In ASP property declarations using GET are written as follows:

 graphics/icon01.gif <%     PUBLIC PROPERTY GET TransactionStatus        TransactionStatus=blnTransactionFLAG     END PROPERTY  %> 

In ASP.NET this code is written as follows:

 graphics/icon01.gif <script language = "vb" runat = "server">     PUBLIC READONLY PROPERTY TransactionStatus        GET       TransactionStatus=blnTransactionFLAG        END GET     END PROPERTY  </script> 

In ASP property declarations using LET are written as follows.

 graphics/icon01.gif <%     PUBLIC PROPERTY LET PageNumber(vPageNumber)     IF vPageNumber="" or vPageNumber=0 Then        intPageNumber=1     ELSE        intPageNumber=vPageNumber     END IF     END PROPERTY  %> 

In ASP.NET this code can be written as

 graphics/icon01.gif <script language = "vb" runat = "server">     PUBLIC WRITEONLY PROPERTY PageNumber LET        IF Value="" or Value=0 Then           intPageNumber=1        ELSE           intPageNumber=Value        END IF     END LET  </script> 

Properties with only a LET method need to be declared as WRITEONLY as shown.

Create Objects

If VBScript is the scripting language for ASP, objects are created with late binding:

 graphics/icon01.gif <%      SET myObj1 = _      server.createObject("Scripting.FileSystemObject")  %> 

In ASP.NET, objects are generally created with early binding:

 graphics/icon01.gif <%@ Import Namespace ="Scripting"%>     <script language = "vb" runat = "server">        Dim myObj1 as Scripting.FileSystemObject        myObj1 = new Scripting.FileSystemObject()     </script> 

In this example, the Scripting runtime ( scrrun.dll ) should be added explicitly to the Web application.

To assign the value of one object to the other, we use the following syntax in ASP:

 <% SET myObj1 = myObj2%> 

Because the keyword SET is not supported in .NET, this line is reduced to

 graphics/icon01.gif <script language = "vb" runat = "server">     myObj1 = myObj2  </script> 

Method Calls

The way in which methods are called in Visual Basic .NET is different from that in VBScript. All the method calls should now be enclosed in parentheses, whereas in VBScript only function calls had to be enclosed in parentheses. The following comparison between ASP and ASP.NET code makes this clear. In ASP it is written as

 graphics/icon01.gif <%      Sub WriteData()        Response.Write "This is data"      End Sub  %> 

In ASP.NET, this is written as

 graphics/icon01.gif <script language = "vb" runat = "server">  Sub WriteData()     Response.Write ("This is data")  End Sub  </script>  CALL WriteData() 

Parameter Arguments

In Visual Basic .NET parameters are now passed by value rather than by reference, which was the default earlier. The following code will not work as expected:

 graphics/icon01.gif <%   Sub MyByRefSub (Value as Integer)     Value = Value + 10  End Sub  Dim x as Integer     x=4     Response.write MyByRefSub(x)  %> 

This will return 13 in ASP, but under ASP.NET, it will return 4. The method declaration has to be changed in ASP.NET as follows:

 graphics/icon01.gif <script language = "vb" runat = "server">  Sub MyByRefSub (ByRef Value as Integer)     Value = Value + 10  End Sub  </script> 

Default Properties

Default properties are no longer supported in .NET. This affects the way in which COM components are accessed. The following ASP code opens a connection and fills a recordset. To read from the recordset, we use the short-cut way of RS("Name"), where Name is the field name in recordset RS:

 graphics/icon01.gif <%     Set Conn = Server.CreateObject("ADODB.Connection")     Conn.Open("TestDB")     Set RS = Conn.Execute("Select * from Products")     Response.Write RS("Name")  %> 

In ASP.NET we have to give the fully qualified version, RS.Fields ("Name"). Value, for reading from the recordset:

 graphics/icon01.gif <script language = "vb" runat = "server">     Conn = Server.CreateObject("ADODB.Connection")     Conn.Open("TestDB")     RS = Conn.Execute("Select * from Products")     Response.Write (RS.Fields("Name").Value)  </script> 

If you have ASP code that relies on a default property that was provided by one of your objects, you will need to change this to explicitly reference the desired property.


Snoops

   
Top


Migrating to. NET. A Pragmatic Path to Visual Basic. NET, Visual C++. NET, and ASP. NET
Migrating to. NET. A Pragmatic Path to Visual Basic. NET, Visual C++. NET, and ASP. NET
ISBN: 131009621
EAN: N/A
Year: 2001
Pages: 149

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