Visual Basic 9.0 cutting-edge broadcast-static (I) local variable type speculation and array Initiators

Source: Internet
Author: User
Tags array definition

Don't be surprised. Every PDC has a forward view of Microsoft's latest product. Therefore, you can understand the message of Visual Basic 2005 before Visual Basic 9.0 is released. Visual Basic 9.0 (codename Visual Basic Orcas) will be released along with the next main version of Visual Stduio (9.0, Orcas), but Microsoft has started planning for new language features for a long time, at the PDC Conference held today (PST September 13), users can not only learn the features of the Next Generation Visual Basic 9.0, but also try the preview version in the hands-on lab. VB Fans and developers who did not attend the PDC Conference can obtain the first Chinese broadcast from my Blog. Note that it is still far from the official release of VB9.0. This series of articles only briefly introduces the content currently published, and does not guarantee that it is consistent with the official version features.

The theme of Visual Basic 9.0 isDataAndDynamic. VB9 tries to enhance the language to obtain an unprecedented development experience of data (including data, databases, and XML in objects. Secondly, Visual Basic tries to draw some excellent features from dynamic languages, making users feel less restricted and more convenient. In addition, there are some static features unrelated to Data. I plan to divide this article into dynamic and static articles to introduce Visual Basic 9.0's development experience in almost two worlds.

The first new feature of Visual Basic 9.0 I want to introduce isPrediction of local variable types. In earlier versions, when declaring variables, we must use the As statement to specify the variable type.

 Dim aInt As Integer = 5Dim aDbl As Double = 3.33Dim aStr As String = "Hello" Dim aLst As List (Of Integer) = anotherList. convertAll (Of Integer) (PStr. length) 'vbf syntax

We observe these declarations that their initialization statements contain enough information to determine the return type. For example, "5" is determined to be Integer. Therefore, in Visual Basic 9.0,The type declaration of local variables is exempt from being determined by the initialization statement..

 Dim aInt = 5Dim aDbl = 3.33Dim aStr = "Hello"Dim aLst = anotherList.ConvertAll(Of Integer)(PStr.Length)

In this way, the declaration of variables is fully simplified. Note: The Code seems to be valid in Visual Basic 2005. What is the difference? In this case, type speculation occurs during compilation, which is a type-strong feature. For example, if aInt is initialized to 5, aInt is defined not as an Object type but as a real Integer. If aInt is assigned a value and cannot be converted to an Integer value, a compilation error occurs. In Visual Basic 2005, if Option Strict Off is used, the Object type is set by default without the As statement. This rule is no longer valid when it reaches Visual Basic 9.0, to declare the Object type, you must explicitly specify As Object. Note that the last one, we can see that the type real parameters of the generic type are also determined along with the type speculation, So we no longer need to think about the specific type real parameters of the returned type before writing.

Visual Basic 9.0 also supports automatic prediction of loop variable types For Each loops.

 Dim list As List (Of ALongNameStructure) =... For EachDimItem In list 'do not need to write As alongnamestructuring 'Use listNext

Note that the Dim statement helps to speculate on the member type in the list, so you do not need to manually specify it.

In order to let the type speculation work for the array, VB9 also introduced a new simplified array literal, now the array can automatically speculate its type through initialization.

 'Vb2005 array definition and initialization both require the specified type Dim aIntArr As Integer () = New Integer () {1, 2, 3} 'vb 9.0 everything is automatically inferred Dim aIntArr = {1, 2, 3} '. You can also write a bracket Dim strArr () = {"a", "bb ", "ccc"} 'array can also be Dim aIntArrArr = {1, 2}, {3, 4}'. Note that if the types are not uniform, the array is inferred to be a base class, the following example shows the Object array Dim objArr = {1, "2", 3.1}

There is a limit on automatic type speculation. It can only speculate on local (non-Static) variables, and does not support speculation on class-level variables, even if there is an initialization statement.

As the basis for the new features of VB9 data, the speculation of local variable types is a very important feature, but it seems unnecessary because it is very simple at first. Therefore, you only need to remember it first. When you see my subsequent articles, you will understand its usefulness.

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.