我想将Sorted
定义为ErrorProviderMessageCollection
类型,unsortedCollection
定义为。{/ p>
Dim Sorted As ErrorProviderMessageCollection = From item In unsortedCollection
Order By item.Control.TabIndex
我该怎么做?
Public Class ErrorProviderMessage Implements IComparable(Of ErrorProviderMessage)
Private _Message As String
Private _Control As Control
Public Sub New(ByVal message As String, ByVal control As Control)
_Message = message
_Control = control
End Sub
Public ReadOnly Property Message() As String
Get
Return _Message
End Get
End Property
Public ReadOnly Property Control() As Control
Get
Return _Control
End Get
End Property
Public Function CompareTo(ByVal other As ErrorProviderMessage) As Integer Implements System.IComparable(Of ErrorProviderMessage).CompareTo
Return Me.Control.TabIndex.CompareTo(other.Control.TabIndex)
End Function
End Class
Public Class ErrorProviderMessageCollection
Inherits System.Collections.ObjectModel.Collection(Of ErrorProviderMessage)
End Class
答案 0 :(得分:1)
您还可以使用Lambda和适当的扩展方法来处理转换,例如:
var Sorted = unsortedCollection.OrderBy(i=>i.Control.TabIndex).ToSortedErrorProviderMessageCollection();
和扩展方法......
public static class ErrorProviderMessageCollectionHelper
{
public static ErrorProviderMessageCollection ToSortedErrorProviderMessageCollection <T>(this IEnumerable<T> source) where T : ErrorProviderMessage
{
var orderedCollection = new ErrorProviderMessageCollection();
foreach (var item in source)
{
orderedCollection.Add(item);
}
return orderedCollection;
}
}
我没有使用VB.NET,所以在C#中提供代码示例。
答案 1 :(得分:0)
唯一的方法是这样做:
Dim Sorted As ErrorProviderMessageCollection =
New ErrorProviderMessageCollection(From item In unsortedCollection
Order By item.Control.TabIndex)
要使其工作,ErrorProviderMessageCollection
需要一个带有IEnumerable(Of TypeOfItem)
类型参数的构造函数。
答案 2 :(得分:0)
正如批准的答案中所建议的,这很有效。这里显示的工作示例:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyCollection As New ErrorProviderMessageCollection
If txtLastName.Text.Trim = "" Then
MyCollection.Add(New ErrorProviderMessage("Last Name required", txtLastName, txtLastName.TabIndex))
End If
If txtFirstName.Text.Trim = "" Then
MyCollection.Add(New ErrorProviderMessage("First Name required", txtFirstName, txtFirstName.TabIndex))
End If
Dim Sorted As ErrorProviderMessageCollection = New ErrorProviderMessageCollection(From item In MyCollection Order By item.Control.TabIndex)
For Each ErrorProviderMessage As ErrorProviderMessage In Sorted
MessageBox.Show(ErrorProviderMessage.Message & " " & ErrorProviderMessage.Control.TabIndex & " " & ErrorProviderMessage.Control.Name)
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Imports System.ComponentModel
Public Class ErrorProviderMessage
Private _Message As String
Private _Control As Control
Private _TabIndex As Integer
Public Sub New(ByVal message As String, ByVal control As Control, ByVal tabIndex As Integer)
_Message = message
_Control = control
_TabIndex = tabIndex
End Sub
Public ReadOnly Property Message() As String
Get
Return _Message
End Get
End Property
Public ReadOnly Property Control() As Control
Get
Return _Control
End Get
End Property
Public Property Tabindex() As Integer
Get
Return _TabIndex
End Get
Set(ByVal value As Integer)
_TabIndex = value
End Set
End Property
End Class
Public Class ErrorProviderMessageCollection
Inherits System.Collections.ObjectModel.Collection(Of ErrorProviderMessage)
Public Sub New()
End Sub
Public Sub New(ByVal source As IEnumerable(Of ErrorProviderMessage))
Dim orderedCollection = New ErrorProviderMessageCollection()
For Each Item As ErrorProviderMessage In source
Me.Add(Item)
Next
End Sub
End Class