Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the initialization of sets and arrays in Visual Basic 10

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to achieve Visual Basic 10 sets and array initialization, the content is very detailed, interested friends can refer to, hope to be helpful to you.

With the release of. NET 4.0 and Visual Studio 2010, Visual Basic 10 adds support for collection and array initializers. These features are very similar to those of C#, with only minor improvements to extension methods and type references.

Collection initializer

Like C #, Visual Basic's collection initializer is used for classes that implement the IEnumerable interface and expose Add methods. But unlike C #, Add methods can be defined in extension methods.

Var x As new List () {"Item1", "Item2"} Dim x As New List (Of String) From {"Item1", "Item2"}

Passing multiple parameters to the Add method is also very similar to C #.

Var x = new Dictionary () {{1, "Item1"}, {2, "Item2"}} Dim x As New Dictionary (Of Integer, String) From {{1, "Item1"}, {2, "Item2"}}

There is a slight ambiguity in the C # syntax, which makes it possible to associate property initializers with object initializers. By using the keywords With and From, one might assume that VB can overcome this limitation and write the two initializations in the same statement. Unfortunately, the situation is not that simple. Grammatical problems will arise in the following sentences:

Dim x as New List (Of Integer) With {.Capacity = 10} From {1m 2pm 3}

Another aspect of VB learning about C # is the way exceptions are handled. If an exception is thrown when adding any items to a particular collection, the entire operation is aborted and the value of the collection variable does not change.

Array initializer

Numeric initializers now support type references, which greatly reduces the amount of code. As we see next in the code, the array values in curly braces need to infer whether or not to create and type the array.

Dim x = {1,2,3}

By contrast, earlier versions of VB required empty parentheses to represent arrays. Moreover, if not explicitly specified, the variable is defined as an array of objects.

Dim x As Integer () = {1,2,3} 'integer array Dim x () = {1,2,3}' object array

Both multidimensional arrays and interlaced arrays are supported, although the syntax of the latter (parentheses for each array) is a bit clumsy.

Dim multi = {{1,2}, {3,4}} Dim jagged () () = {({1,2}), ({3,4,5})}

Array initializers can also be used inline in calling functions.

On how to achieve Visual Basic 10 sets and array initialization is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report