Vb. List of useful common objects in net

Source: Internet
Author: User
Tags foreach sort

To use list (of T), we need to know how to deploy. NET Framework provides a variety of methods. This is what this article will be about. I've written three examples--using foreach, FindAll, and sort--to illustrate how the same list class works.

The first step is to create a shared list. You can get data from a variety of sources, but the easiest way is to add it. Text, we'll write code to categorize the set in the example. So, let's talk about the code that creates the set.

First, I need an object that can represent the bottle of a set. The code written for this is perfectly compliant, and in fact, VB.net 2008 Express IntelliSense will write most of the code for you. The following are my objects:

Public Class Bottle The "internalproperties" 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 these are standard codes in vb.net1.0. However, it is to be noted by defining our own bottle object. We will benefit from multiple types in the same concentration.

Next we'll introduce the foreach, FindAll, and sort methods.

When we use these methods, we find the fun. First, let's deploy the Foreach method. The Microsoft file contains a syntax definition for its use.

Dim instance As List Dim action As Action (of T) instance. ForEach (Action)

Microsoft further defines the delegation behavior as a way of demonstrating the behavior that can be passed by an object. The current list (t) element is transmitted individually to the action (t) representation.

The first thing to do is to write the 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 encodings for the type of object are completed. When we can use this function correctly, the process is simple. In our example, the use of it is very simple. The entire example of bottle is routed, and the subroutine will pick up any required data from it.

Sub Displaybottle (ByVal B as Bottle) Console.WriteLine (B.brand & "-" & b.name) End Sub

Writing the Foreach method itself is simple enough to fill in the address of the representative.

Cabinet.foreach (AddressOf displaybottle)

FindAll a little bit complicated. Microsoft's note on 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 criteria and a way to determine whether the specified object meets these criteria. In other words, we can create any code that can find data in a list. The predicate (of T) I wrote can search for beer species:

Function Findbeer (ByVal B as bottle) _ as booleanif (b.category = "Beer") Then

Return True

Else

return FalseEnd IfEnd Function

FindAll returns the entire list (T) instead of the representation code for each item in the call list. This list (t) contains only data that matches predicate (t). The definition and operation of the second list is also dependent on the code we write. Again, my code is simplified to avoid omissions.

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

The last method discussed in this article is sort. Microsoft has used some terminology that you may not be familiar with to explain it. There are actually four different sort method loads:

1. Sort ()

2. Sort (IComparer (T)

3. Sort (Comparison (T) 4. Sort (Int32,int32,icomparer (T)

This allows us to use. NET Framework to write your own code or simply to collect part of a collection by using the start position and count parameters.

In this example, I wrote another representative for my comparer. Because I wanted to sort by my classification, I just smoked the value of each example in the bottle object that was sent.

Private Shared Function Sortcabinet (_ ByVal x as bottle, ByVal y as bottle) as Integerreturn X.category.compareto (Y.categ Ory)

End Function

The sort method actually rearranges the original list (T). So that's one of the procedures that happens after this method is executed.

Cabinet.sort (AddressOf sortcabinet) for the each result as bottle in Cabinetconsole.writeline (result. Brand & "-" & result. Name)

Next

These methods are chosen to describe the main ways in which the framework method code is written in list (T). You will find that they make the list (T) more useful.

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.