List of useful general objects in VB. NET

Source: Internet
Author: User

To use List (of T), we need to know how to deploy the. NET Framework. This will also be the content of this article. I have compiled 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 obtain data in multiple ways, but the simplest way is to add data. In the text, we will write code to classify the set in the example. Therefore, let's talk about the code for creating a set.
First, I need an object that can represent the Bottle in the set. For this reason, the Code is fully compliant with the standard, and in fact, VB. NET 2008 Express intelliisense will write most of the code for you. The following are my objects:
Public Class Bottle
"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, we need to specify it by defining our own Bottle object. We will benefit from multiple types in the same set.
The following describes the ForEach, FindAll, and Sort methods.
When we use these methods, we will find the fun. First, deploy the ForEach method. Microsoft files contain their syntax definitions.
Dim instance As List
Dim action As Action (Of T)
Instance. ForEach (action)
Microsoft further defines delegated behavior as a way to demonstrate the behavior passed by objects. Currently, the List (T) element is transmitted to Action (T) separately.
The first thing to do is to write the code of the delegate method. Most people are confused about this key point. This function or subroutine is the place where all custom codes for Of type objects are completed. When we can use this function correctly, the process is very simple. In our example, it is easy to use. The entire Bottle example is transmitted, and the subroutine selects any required data from it.
Sub displayBottle (ByVal B As Bottle)
Console. WriteLine (B. Brand & "-" & B. Name)
End Sub
Writing the ForEach method is very simple. You only need to enter the representative address.
Cabinet. ForEach (AddressOf displayBottle)
FindAll is a little complicated. Microsoft's description 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 we see different elements in our syntax, Predicate (T ). According to Microsoft,
This defines a set of standards and methods to determine whether the specified object meets these standards. In other words, we can create any code that can search for data in the list. The Predicate (Of T) compiled by me can search for Beer types:
Function findBeer (ByVal B As Bottle )_
As Boolean
If (B. Category = "Beer") Then
Return True
Else
Return False
End If
End Function
FindAll returns the entire List (T) instead of the code of each project in the call List. This List (T) only contains data that matches Predicate (T. The definition and operation of the second List (T) also depend on the code we compile. Once again, my code is simplified to avoid complexity.
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 terms that you may not be familiar with to explain them. 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 write our own code using the Sort method defined in the. NET Framework, or collect a portion of the set by using the start bit and count parameters.
In this example, I wrote another example for my comparator. Because I want to use my classification method to classify, I just extract the values of each example in the sent Bottle object.
Private Shared Function sortCabinet (_
ByVal x As Bottle, ByVal y As Bottle) As Integer
Return x. Category. CompareTo (y. Category)
End Function
The Sort method re-sorts the original List (T. So this is the process that occurs after this method is executed.
Cabinet. Sort (AddressOf sortCabinet)
For Each result As Bottle In Cabinet
Console. WriteLine (result. Brand & "-" & result. Name)
Next
These methods are used to illustrate the main way to compile the framework method code in List (T. You will find that they make List (T) more useful.

Related Article

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.