Microsoft translation of free book "Introducing Microsoft LINQ" chapter3.1-Visual Basic 9.0 Language Features

Source: Internet
Author: User

This book is intended for personal learning and knowledge sharing. The copyright of this book is owned by the original author. If there is any infringement, please inform me that I will handle the post immediately.
Reprinting is allowed, but this copyright statement is not allowed for commercial purposes!
Blog: Han xilong

Introducing to Microsoft LINQ directory

The final version of Microsoft Visual basic9.0 enhances support for language integration query (LINQ. As in C #3.0, visual basic9.0 changes do not need to be made to the Common Language Runtime (CLR.

For more information about C #3.0, see section 2nd "C # language features"

In this chapter, we will introduce the new syntax in visual basic9.0 and compare it with C # when appropriate. There are some differences between C # And vb9, and some features are not in either of them. Even as a C # programmer, read this chapter. Some advantages of Visual Basic 9.0 may not exist in C. At least we should be able to understand the Visual Basic code written for LINQ.

If you prefer Visual Basic and want to fully and efficiently understand vb9, you must have a comprehensive understanding of the features of vb8. The ability to read C # code is also important for reading other parts of this book (because the examples of LINQ are written in C #) and understanding the differences between C #3.0 and Visual Basic 9.0.

Visualized basic 9.0And nullable type

Visual Basic 9.0 includes features that already exist in C #2.0. One of these features is the void type, which is also frequently used in LINQ. Therefore, we need to quickly describe the null type.

From the Introduction to Microsoft. net2.0 generics, CLR provides programmers with generic classes.Nullable (of T as struct)This satisfies the requirements of programmers to assign null values to a value type. When declaring a variable of this type, we do not have to specifically declare a new type for it.

To use this type, C #2.0 provides direct support for it. Set?We can use a type that can be empty before prefix to type name. Visual basic9.0 also provides the same functionality with the same syntax, as shown in the Listing3-1:

Listing 3-1: nullable type declaration



Dim A as integer? = 18

Dim B as integer? = 24

Dim C as integer? = Nothing

Dim D as integer?

D = A + C 'd = 18 + nothing = nothing

C = a + B 'c = 18 + 24 = 42

CommonIntegerVariable cannot be specifiedNothingBecause it is of the "value type. In Listing3-1, we willCSpecifiedNothingAnd assign itD.A + CThe result isNoting,This shows that the three-value logic is a typical void type. Null "value" (used in Visual BasicNothingAnd some exceptions using logical operators have been applied to the expressions.

An empty value cannot be assigned to a non-nullable type ). The Listing3-2 shows the transformations necessary to assign an empty value to a non-empty type:

Listing 3-2: nullable type conversions

 
Dim K as integer? = 16

Dim P as integer = K' compiler Error

Dim Q As integer = directcast (K, integer) 'OK

Dim R as integer = ctype (K, integer) 'OK

For more information about the null type in Visual Basic 9.0, see the Visual Basic documentation. We hope this feature is widely used in the code that operates relational databases.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.