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

What are the practical skills of VB.NET List

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is to share with you about the practical skills of VB.NET List. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

To use VB.NET List, we need to understand how to deploy the various methods provided by the .NET Framework. This will also be the content of this article. I've written three examples-using ForEach, FindAll, and Sort-- to illustrate how the same list class works.

The * step is to create a sharing list. You can get data from many ways, but the easiest way is to add. In the text, we will write code to classify the sets in the example. So, let's talk about the code that creates the set.

First, I need an object that represents the Bottle in the set. The code written for this is fully standard, and in fact, VB.NET 2008 Express Intellisense will write most of the code for you. The following is my partner:

Public Class Bottle "internal

Properties "Public Property

Brand () As String

Public Property Name ()

As String

Public Property Category ()

As String

Public Property Size ()

As Decimal

Public Sub New (_

End Sub

End Class

To create a set, I need to add a project:

Dim Cabinet As List (Of Bottle) =

_ "New List (Of Bottle) Cabinet.

Add (New Bottle (_

"Castle Creek", _

"Uintah Blanc", _

"Wine", 750)

Cabinet.Add (New Bottle (_

"Zion Canyon Brewing Company", _

"Springdale Amber Ale", _

"Beer", 355)

Cabinet.Add (New Bottle (_

"Spanish Valley Vineyards", _

"Syrah", _

"Wine", 750)

Cabinet.Add (New Bottle (_

"Wasatch Beers", _

"Polygamy Porter", _

"Beer", 355) Cabinet.

Add (New Bottle (_

"Squatters Beer", _

"Provo Girl Pilsner", _

"Beer", 355)

All of this is standard code in VB.NET1.0. However, you need to specify it by defining our own Bottle object. We will benefit from multiple types in the same set.

Next we will introduce the ForEach, FindAll, and Sort methods.

When we use these methods, we will find the fun. First, let's deploy the ForEach method. The Microsoft file contains the syntax definition of its use.

Dim instance As List

Dim action As Action (Of T)

Instance.ForEach (action)

Microsoft further defines delegation behavior as a way to demonstrate the behavior passed by an object. The current VB.NET List elements are transferred separately to the Action (T) representation.

The only thing to do is to write code for the delegated method. The misunderstanding of this key point is the reason why most people are confused about VB.NET. This feature or subroutine is the place where all custom coding for objects of type Of is done. When we can use this feature correctly, the process is very simple. In our example, the use of it is simple. The entire example of Bottle is passed, and the subroutine picks out any data it needs.

Sub displayBottle

(ByVal b As Bottle)

Console.WriteLine

(b.Brand & "-" & b.Name)

End Sub

Writing the ForEach method itself is simple, just fill in the address of the representative.

Cabinet.ForEach

(AddressOf displayBottle)

FindAll is a little more complicated. Microsoft's explanation of FindAll is as follows:

Dim instance As List

Dim match As Predicate (Of T)

Dim returnValue As List (Of T)

ReturnValue = instance.

FindAll (match)

Now, there are different elements in our syntax, Predicate (T). According to Microsoft, this will represent a set of standards defined and a way to determine whether a given object conforms to those standards. In other words, we can create any code that can find data in the list. The Predicate (Of T) I wrote can search for Beer categories:

Function findBeer (ByVal

B As Bottle)

_ As BooleanIf

(b.Category = "Beer") Then

Return True

Else

Return FalseEnd IfEnd Function

FindAll returns the entire VB.NET List rather than the representative code for each item in the call list. This VB.NET List) contains only data that matches Predicate (T). The definition and operation of the second VB.NET List also depends on the code we write. I repeat, my code is simplified to avoid redundancy.

Dim sublist As List (Of Bottle)

Sublist = Cabinet.FindAll

(AddressOf findBeer)

For Each result As Bottle

In sublist

Console.WriteLine (result.

Brand & "-" & result.Name)

Next

One of the methods discussed in this article is Sort. Microsoft uses terms that you may not be familiar with to explain it. There are actually four different Sort method payloads:

1. Sort ()

2. Sort (Icomparer (T)

3. Sort (Comparison (T)

4. Sort (Int32,Int32,Icomparer (T)

This allows us to write our own code using the Sort methods defined in the .NET Framework or simply collect part of the collection by using the starting position and count parameters.

In this example, I wrote another representative for my comparator. Since I want to classify through my classification method, I just extract the value of each instance in the Bottle object that is passed.

Private Shared Function

SortCabinet (_ ByVal x As

Bottle, ByVal y As Bottle)

As IntegerReturn

X.Category.CompareTo

(y.Category)

End Function

The Sort method actually rearranges the original VB.NET List. So this is a process that happens after the method is executed.

Cabinet.Sort (AddressOf

SortCabinet)

For Each result As Bottle

In CabinetConsole.WriteLine

(result.Brand & "-"

& result.Name)

Next

These methods are selected to illustrate the main ways of writing framework method code in VB.NET List. You will find that they make VB.NET List more useful.

Thank you for reading! This is the end of this article on "what are the practical skills of VB.NET List?". I hope the above content can be of some help to you, so that 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