vb.net 串口列表_VB.NET中的有用的通用列表

vb.net 串口列表

Generics extend the power and flexibility of VB.NET in a lot of areas, but you get a bigger performance benefit and more programming options in the generic List object [List(Of T)] than with any other.

泛型在很多方面扩展了VB.NET的功能和灵活性,但是与其他任何类型相比,泛型List对象[ List(Of T) ]都具有更大的性能优势和更多的编程选项。

To use List(Of T), you have to understand how to implement the many methods that the .NET Framework provides. Below are three examples using ForEach, FindAll, and Sort, that demonstrates how the generic List class works.

若要使用List(Of T) ,您必须了解如何实现.NET Framework提供的许多方法。 以下是使用ForEachFindAllSort的三个示例,它们演示了通用List类的工作方式。

The very first step is to create a generic List. You can get the data in a lot of ways, but the simplest is to just Add it. The code below shows how to classify my beer and wine collection!

第一步是创建一个通用List 。 您可以通过多种方式获取数据,但最简单的方法就是添加数据 。 下面的代码显示了如何对我的啤酒和葡萄酒收藏进行分类!

起始码 ( Starting Code )

There first needs to be an object that will represent a bottle from the collection. In a Windows Forms application, the Form class has to first be in a file or the Visual Studio designer won't work correctly, so put this at the end:

首先需要一个对象,该对象将代表集合中的瓶子。 在Windows Forms应用程序中,Form类必须首先位于文件中,否则Visual Studio设计器将无法正常工作,因此请在结尾处输入以下内容:

Public Class Bottle
Public Brand As String
Public Name As String
Public Category As String
Public Size As Decimal
Public Sub New( _
ByVal m_Brand As String, _
ByVal m_Name As String, _
ByVal m_Category As String, _
ByVal m_Size As Decimal)
Brand = m_Brand
Name = m_Name
Category = m_Category
Size = m_Size
End Sub
End Class

To build the collection, Add the items. This is what's in the Form Load event:

要构建集合,请添加项目。 这是“ 表单加载”事件中的内容:

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 the above code is standard code in VB.NET 1.0. However, note that by defining your own Bottle object, you get the benefits of multiple types in the same collection (in this case, both String and Decimal) and efficient, type safe "late binding."

以上所有代码都是VB.NET 1.0中的标准代码。 但是,请注意,通过定义自己的Bottle对象,可以在同一个集合中获得多种类型的好处(在本例中为StringDecimal ),并且可以进行有效的类型安全的“后期绑定”。

每个例子 ( ForEach Example )

The fun starts when we use the methods. To begin, let's implement the familiar ForEach method. The Microsoft documentation includes this usage syntax definition:

当我们使用这些方法时,乐趣就开始了。 首先,让我们实现熟悉的ForEach方法。 Microsoft文档包括以下用法语法定义:

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

Microsoft further defines action as "delegate to a method that performs an action on the object passed to it. The elements of the current List(T) are individually passed to the Action(T) delegate."

Microsoft进一步将动作定义为“委托对传递给它的对象执行动作的方法。将当前List(T)的元素分别传递给Action(T)委托”。

Tip: For more on delegates, read Using Delegates in Visual Basic .NET for Runtime Flexibility.

提示:有关委托的更多信息,请阅读在Visual Basic .NET中为运行时灵活性使用委托 。

The first thing you need to code is the method that will be delegated. Misunderstanding this one key point is the source of most of the confusion of VB.NET students. This function, or subroutine, is where all of the customized coding for the "Of " type objects is done.

您需要编写的第一件事是要委派的方法。 对这一关键点的误解是大多数 VB.NET学生困惑的根源。 此函数或子例程是完成“ Of”类型对象的所有自定义编码的地方。

When performed correctly, you're essentially done. It's really simple in this first example. An entire instance of the Bottle is passed and the subroutine selects anything needed out of it. Coding the ForEach itself is simple too. Just fill in the address of the delegate using the AddressOf method.

如果正确执行,则基本上就可以完成。 在第一个示例中,这非常简单。 传递了Bottle的整个实例,并且子例程从其中选择所需的任何东西。 编码ForEach本身也很简单。 只需使用AddressOf方法填写委托人的地址。

Sub displayBottle(ByVal b As Bottle) ResultList.Items.Add( _ b.Brand & " - " & _ b.Name & " - " & _ b.Category & " - " & _ b.Size) End Sub Private Sub ForEachButton_Click( ... ResultList.Items.Clear() ResultList.Items.Add("For Each Example") ResultList.Items.Add("-----------------------") Cabinet.ForEach(AddressOf displayBottle) End Sub

FindAll示例 ( FindAll Example )

FindAll is a little more complicated. The Microsoft documentation for FindAll looks like this:

FindAll有点复杂。 Microsoft的FindAll文档如下所示:

Dim instance As List Dim match As Predicate(Of T) Dim returnValue As List(Of T) returnValue = instance.FindAll(match)

This syntax includes a new element, Predicate(Of T). According to Microsoft, this will represent the method "that defines a set of criteria and determines whether the specified object meets those criteria." In other words, you can create any code that will find something in the list. I coded my Predicate(Of T) to find anything in the "Beer" Category.

此语法包括一个新元素Predicate(Of T) 。 根据Microsoft的说法,这将表示“定义一组条件并确定指定对象是否满足这些条件的方法”。 换句话说,您可以创建将在列表中找到内容的任何代码。 我将Predicate(Of T)编码为“啤酒” 类别中的任何内容。

Instead of calling the delegate code for each item in the list, FindAll returns an entire List(T) containing only the matches that result from your Predicate(Of T). It's up to your code to both define this second List(T) and do something with it. My code just adds the items to a ListBox.

FindAll返回一个完整的List(T)而不包含列表中每个项目的委托代码,该列表仅包含Predicate(Of T)产生的匹配项。 定义第二个List(T)并对其进行处理取决于您的代码。 我的代码只是将项目添加到ListBox

Private Sub FindAllButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindAllButton.Click ResultList.Items.Clear() ResultList.Items.Add("FindAll Example") ResultList.Items.Add("-----------------------") Dim sublist As List(Of Bottle) sublist = Cabinet.FindAll(AddressOf findBeer) For Each r As Bottle In sublist ResultList.Items.Add( _ r.Brand & " - " & _ r.Name & " - " & _ r.Category & " - " & _ r.Size) Next End Sub Function findBeer(ByVal b As Bottle) _ As Boolean If (b.Category = "Beer") Then Return True Else Return False End If End Function

排序范例 ( Sort Example )

The final method this article examines is Sort. Again, Microsoft uses some terminology you might not be familiar with. There are actually four different overloads of the Sort method:

本文研究的最终方法是Sort 。 同样,Microsoft使用一些您可能不熟悉的术语。 实际上, Sort方法有四个不同的重载:

  • Sort()

    分类()

  • Sort(IComparer(T))

    排序(IComparer(T))

  • Sort(Comparison(T))

    排序(比较(T))

  • Sort(Int32, Int32, IComparer(T))

    排序(Int32,Int32,IComparer(T))

This lets you use sort methods defined in the .NET Framework for the list, code your own, use a system defined comparison for the type, or sort part of the collection using a starting position and count parameter.

这使您可以将.NET Framework中定义的排序方法用于列表,编写自己的代码,对类型使用系统定义的比较或使用起始位置和计数参数对集合的一部分进行排序。

In this example, since I use the following syntax to actually perform the sort, I'm using the third overload.

在此示例中,由于我使用以下语法实际执行排序,因此我在使用第三重载。

x.Name.x.Name.CompareTo(y.Name)(y.Name)

I've coded another delegate to my own comparer. Since I want to sort by my Name, I pull just that value out of each instance of the Bottle object that is passed and use the Sort(Comparison<(Of <(T>)>)). The Sort method actually rearranges the original List(T). That's what is processed after the method is executed.

我已经将另一个委托编码为我自己的比较器。 由于我想按名称进行排序,因此只从传递的Bottle对象的每个实例中拉出该值,然后使用Sort(Comparison <(Of <(T>)>))Sort方法实际上重新排列了原始List(T) 。 该方法执行后即进行处理。

Private Sub SortButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SortButton.Click ResultList.Items.Clear() ResultList.Items.Add("Sort Example") ResultList.Items.Add("-----------------------") Cabinet.Sort(AddressOf sortCabinet) For Each r As Bottle In Cabinet ResultList.Items.Add( _ r.Name & " - " & _ r.Brand & " - " & _ r.Category & " - " & _ r.Size) Next End Sub Private Shared Function sortCabinet( _ ByVal x As Bottle, ByVal y As Bottle) As Integer Return x.Name.CompareTo(y.Name) End Function

These methods were selected to demonstrate the major ways that the Framework methods in List(T) are actually coded. There's a whole raft of other methods, however. That's what makes List(T) so useful!

选择这些方法是为了演示List(T)中的Framework方法实际编码的主要方法。 但是,还有很多其他方法。 这就是使List(T)如此有用的原因!

翻译自: https://www.thoughtco.com/the-useful-generic-list-in-vbnet-3424414

vb.net 串口列表

你可能感兴趣的:(python,java,编程语言,linux,人工智能)