Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET 2.0 Beta Preview - B. Evjen.pdf
Скачиваний:
26
Добавлен:
24.05.2014
Размер:
15.33 Mб
Скачать

Chapter 15

Listing 15-16: Visual Basic code with comments for XML documentation

Imports Microsoft.VisualBasic

‘’’ <summary>My Calculator Class</summary> Public Class Class1

‘’’ <summary>This Add method returns the value of two numbers ‘’’ added together</summary>

‘’’ <param name=”a”>First number of the collection of numbers to

‘’’ be added</param>

‘’’ <param name=”b”>Second number of the collection of numbers to ‘’’ be added</param>

Public Function Add(ByVal a As Integer, ByVal b As Integer) Return (a + b)

End Function

End Class

The Visual Basic 8.0 compiler now includes a new /doc command that is similar to the way C# works with XML documentation. Compiling your VB code using the /doc command causes the compiler to produce the XML documentation with the compilation.

New Visual Basic Keywords

Visual Basic 8.0 introduces a couple of new keywords that can be utilized in your ASP.NET 2.0 applications. The keywords were brought to the language to make it easier to perform some common tasks, such as working in loops or destroying resources as early as possible. Look at a couple of the new additions to the Visual Basic language.

Continue

The Continue statement is an outstanding new addition to the Visual Basic language that was brought on board to enable you to work through loops more logically in some specific situations. When working in a loop, it is sometimes beneficial to stop the conditional flow and move onto the next item in the collection if the item being examined simply doesn’t fit your criteria. This logic can now be implemented better because of the new Continue statement. Listing 15-17 shows an example of the use of the Continue statement.

Listing 15-17: Using the Continue statement

Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim myString As String

Dim count As Integer = 0

myString = “The St. Louis Rams will go to the Superbowl this year.”

For i As Integer = 0 To (myString.Length() - 1) If (myString(i).Equals(“ “c)) Then Continue For

count += 1

426

Visual Basic 8.0 and C# 2.0 Language Enhancements

Next

Label1.Text = “There are “ & count.ToString() & _ “ characters used (minus spaces).”

End Sub

This little example takes a String and counts each character in the complete string that is not a space. If a space is encountered, the Continue statement finds this in the check and immediately stops execution of the loop for that particular item in the collection. It then hands over the execution of the loop to the next item in the collection. In this example, you could easily check for the characters with a nested If statement, but using multiple nested If statements can get confusing sometimes. The use of the Continue statement makes the logic contained within the For loop very evident and clean.

The Continue statement is not only meant to be used within a For loop, but you can also use this new keyword with other language features that loop through a collection of items — such as the Do and While statements. The following example shows how to use the Continue statement with the four available options:

For [statement]

...

If [statement] Then Continue For

...

Next

For Each [statement]

...

If [statement] Then Continue For

...

Next

Do While [statement]

...

If [statement] Then Continue Do

...

Loop

While [statement]

...

If [statement] Then Continue While

...

End While

As you can see, you have many ways to use this new keyword to make your code easier to read and manage.

427

Chapter 15

Using

Although the using keyword in C# is quite prevalent (one use being to import namespaces into a class), the new Visual Basic Using keyword should not be confused with this C# version. It is similar to the C# using statement that defines the scope of an object.

You use the Using keyword in Visual Basic to ensure that expensive resources get destroyed as soon as possible instead of allowing them to sit in memory until the method is executed. You can now destroy expensive resources, such as connection objects and COM wrappers, immediately when you have finished using them instead of waiting for the garbage collector to come by and make its rounds. An example of working with the Using keyword is illustrated in Listing 15-18.

Listing 15-18: Working with the Using keyword

Using myConn As New SqlConnection

‘ Work with the SqlConnection object

End Using

In Listing 15-18, you can see that instead of using the Dim keyword to create a new instance of the SqlConnection object, the Using keyword is used in its place. If you utilize the Using keyword, you must close the Using statement with an End Using statement. The End Using statement is located at the point where the SqlConnection object is destroyed from memory.

My

My, oh my, what a new keyword! The My keyword is a novel concept to quickly give you access to your application, the computer, or the network in which the application resides. The My keyword has been referred to as a way of speed-dialing common but complicated resources that you need access to. Using the My keyword, you can quickly get access to a wide variety of items such as user details or specific settings of the requestor’s browser.

If you type the My keyword into your application, you will notice that IntelliSense provides you with three items to work with: Application, Computer, and User. Although this new keyword works best in the Windows Forms environment, there are still things you can use in the Web Forms world.

If you want to get at the identity of a user, for example, you can use the following construct:

Label1.Text = My.User.Identity.Name.ToString()

Another example is checking whether the browser making the request to the application is a mobile device. For this, you would use the following construction:

Label1.Text = My.Application.Request.Browser.IsMobileDevice.ToString()

You can get to the information stored in files, system settings, and more in a number of quick ways. The best way to explore the My namespace is to look through IntelliSense and see what is available.

428

Visual Basic 8.0 and C# 2.0 Language Enhancements

Global

The Global keyword was added as a top-root namespace to avoid any namespace conflicts that might arise from similarly named namespaces. Here is an example of how you can use the Global keyword:

Global.System.String

Summar y

This short chapter looked at some of the changes to the C# and Visual Basic languages in their latest releases. When a new version of the .NET Framework comes along, everything associated with it is refreshed at the same time. With .NET Framework 2.0, not only do you get a new version of ASP.NET, but new versions of all the Microsoft .NET-compliant languages, Windows Forms, and more.

The new features of C# and VB illustrated in this chapter can be directly used in the ASP.NET 2.0 applications you are building — giving you applications that contain cleaner and better-performing code.

429