In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the VB. NET object list usage examples, with a certain reference value, interested friends can refer to, I hope you read this article after a lot of gains, the following let Xiaobian with everyone to understand.
VB. NET Object List *** Step is to create a list of commonalities. You can get data from many sources, but the easiest way is to add it. Text, we'll write code to classify the sets in the example. So let's talk about the code that creates the set. First of all, I need an object that can represent a bottle in a collection. The code written for this is completely standards-compliant, and in fact VB. NET 2008 Express Intellisense will write most of it for you. Here are my targets:
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 items:
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, note this by defining our own Bottle object. We benefit from multiple types in the same set.
Next we will introduce VB. NET object lists ForEach, FindAll, and Sort methods. When we use these methods, we will find them fun. First, let's deploy the ForEach method. Microsoft documentation contains definitions of its usage syntax.
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 of object passing. The current List(T) element is sent separately to Action(T). *** One thing to do is write code for delegated methods. Misunderstanding of this key point is why most people are confused about VB. NET. This function or subroutine is where all custom coding for objects Of type Of is done. When we can use this function correctly, the process is simple. In our example, its use is simple. The entire sample of Bottle is passed, and the subroutine extracts any data it needs from it.
Sub displayBottle(ByVal b As Bottle) Console.WriteLine(b.Brand & " - " & b.Name)End Sub
VB. NET object list preparation ForEach method, only need to fill in the representative address can be.
Cabinet.ForEach(AddressOf displayBottle)
FindAll is a bit more 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, a different element appears in our syntax, Predicate(T). According to Microsoft, this would mean defining a set of criteria and a method for determining whether a given object meets those criteria. In other words, we can create any code that finds data in lists. I wrote Predicate(Of T) to search for Beer types:
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 calling the representative code for each item in the list. This List(T) contains only data that matches Predicate(T). The definition and operation of the second List(T) also depends on the code we write. Again, 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 explains it in terms you may not be familiar with. There are actually four different Sort method payloads:
Sort() Sort(Icomparer(T) 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 simply collect a portion of the collection by using the start position and count parameters.
In this case, I wrote another representation for my comparator. Since I wanted to classify by my classification, I simply extracted the value of each instance in the Bottle object I 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 List(T). So that's what happens when the method is executed.
Cabinet.Sort(AddressOf sortCabinet) For Each result As Bottle In CabinetConsole.WriteLine(result.Brand & " - " & result.Name) Next
These methods were chosen to illustrate the main ways to code framework methods in List(T). You'll find that they make List(T) more useful.
Thank you for reading this article carefully, hope Xiaobian share "VB. NET object list usage example" This article is helpful to everyone, but also hope that everyone more support, pay attention to the industry information channel, more relevant knowledge waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.