I would like to define Sorted
to be of type ErrorProviderMessageCollection
, which is what unsortedCollection
is defined as.
Dim开发者_JS百科 Sorted As ErrorProviderMessageCollection = From item In unsortedCollection
Order By item.Control.TabIndex
How do I do this?
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
You could also use Lambda and an appropriate extension method to deal with the casting e.g:
var Sorted = unsortedCollection.OrderBy(i=>i.Control.TabIndex).ToSortedErrorProviderMessageCollection();
and the extension method...
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;
}
}
I'm not up on VB.NET so provide the code example in C#.
The only way is to do it like this:
Dim Sorted As ErrorProviderMessageCollection =
New ErrorProviderMessageCollection(From item In unsortedCollection
Order By item.Control.TabIndex)
For this to work ErrorProviderMessageCollection
needs a constructor that takes a parameter of type IEnumerable(Of TypeOfItem)
.
As suggested in the approved answer, this worked. A workinge example shown here:
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
精彩评论