Professional ASP.NET 1.1 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Professional ASP.NET 1.1 [Electronic resources] - نسخه متنی

Alex Homeret

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید






Chapter 2, but let's reiterate some of them here.

Visual Basic probably has the changes that affect people most, purely because most ASP pages are written in VBScript. Microsoft made a brave (but correct) move in updating the language – breaking functionality in certain areas and adding functionality in others. The main reasons for this are to bring Visual Basic into the fold of the .NET CLR, and to take the opportunity to update the language with some much-needed features.

The details listed in Chapter 2 regarding Visual Basic related to the language as a whole. This section looks at the differences between VBScript and Visual Basic .NET and the sort of things you'll need to change when migrating applications.



Variables and Strong Typing


The first point is that VBScript is no longer used as it is replaced by Visual Basic .NET. This means you now have the ability to have data types. For example:


Dim Name As String

Dim Age As Integer


The second point about variables is that by default they have to be declared, as the

explicit option is set to

true . You can set this option on a page-by-page basis by:


<%@ Page Explicit="False" %>


Alternatively it can be set in the

web.config file:


<compilation explicit="false"/>


As a general rule, explicit variable declaration is better, as it reduces the potential for errors.


Variant and Data Type Conversion


The

Variant data type has been removed, and replaced by a generic

Object . For example, in VBScript you could do this:


Dim o

o = 1

Response.Write "o=" & o & "<br/>"

o = "hello"

Response.Write "o=" & o & "<br/>"


And the output would be:

o=1

o=hello

In Visual Basic .NET, you can declare a variable as type

Object , although the effect this has depends on whether you have strict typing enabled or not. This can be set either at the page level with:


<%@ Page Strict="True" %>


or in the

web.config file:


<compilation strict="true"/>


Without strict typing the following code works fine:


Dim o As Object

o = 1

Response.Write("o=" & o & "<br/>")

o = "hello"

Response.Write("o=" & o & "<br/>")


That's because the object type automatically converts its value to a

string . With strict typing, however, the code needs to be changed to:


Dim o As Object

o = 1

Response.Write("o=" & o.ToString() & "<br/>")

o = "hello"

Response.Write("o=" & o.ToString() & "<br/>")


Automatic conversion cannot be done on the

Object type with strict conversion, so you have to convert explicitly. Implicit conversion is limited to widening, for example from an

Integer to a

Long .


Explicit Type Conversion


To perform data type conversion use the

CType function or the

cast keywords:


CType (expression, DataType)


The expression is the variable to be converted, and DataType is the new type. For example:


Dim d As Double = 123.456

Dim i As Integer = CType(d, Integer)


The alternative method of conversion is to use the cast types:


Dim i As Integer = CInt(d)


Since every primitive data type ultimately inherits from

Object , you can also utilize the

ToString() method provided by

Object . For example:


Dim d As Double = 123.456

Response.Write(d.ToString())


There is also a

Convert class (in the

System namespace), which provides methods for data type conversions.


Methods


The main change to methods has been the way in which they are called. In Visual Basic .NET all methods must be surrounded by parentheses, as opposed to VBScript, where parentheses were only required for functions. For example, the following is no longer allowed:


Response.Write "Hello there"


And has to be replaced by:


Response.Write("Hello there")


This has its biggest impact when switching between the ASP and ASP.NET environments.


Method Arguments


By default, arguments to methods in Visual Basic .NET are now passed by value, rather than by reference. Thus the following code will not work as expected:


Sub Foo(X As Integer)

X = X + 1

End Sub

Dim Y As Integer = 3

Foo(Y)

Response.Write("Y=" & Y.ToString())


The output here will be 3, rather than 4. To correct this you need to change the procedure declaration to:


Sub Foo(ByRef X As Integer)



Default Properties


The use of default properties is not allowed in Visual Basic .NET. This doesn't affect .NET components, since there's no way to define a default property, but it has an impact when accessing COM objects. For example, the following would not be allowed:


Dim rs As New ADODB.Recordset

Dim Name As String

rs.Open("...", "...")

Name = rs("Name")


The last line in .NET would be:


Name = rs("Name").Value


This means more typing, but makes the code much more explicit, and therefore less prone to errors.


Set and Let


The

Set and

Let keywords are no longer required for object references. For example, the following is now the accepted syntax:


Dim conn As SQLConnection

conn = SQLConnection



Single and Multiple Lines


In Visual Basic .NET, all

If statements must be constructed across multiple lines. For example, in VBScript you could do this:


If x > y Then foo()


In Visual Basic .NET this becomes:


If x > y Then

foo()

End If


/ 244