如何返回 TYPED 结果?
我想将 Sorted
定义为 ErrorProviderMessageCollection
类型,这就是 unsortedCollection
的定义。
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
I would like to define Sorted
to be of type ErrorProviderMessageCollection
, which is what unsortedCollection
is defined as.
Dim 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以使用 Lambda 和适当的扩展方法来处理转换,例如:
和扩展方法...
我不熟悉 VB.NET,因此请提供 C# 中的代码示例。
You could also use Lambda and an appropriate extension method to deal with the casting e.g:
and the extension method...
I'm not up on VB.NET so provide the code example in C#.
唯一的方法是这样做:
为此,
ErrorProviderMessageCollection
需要一个采用IEnumerable(Of TypeOfItem)
类型参数的构造函数。The only way is to do it like this:
For this to work
ErrorProviderMessageCollection
needs a constructor that takes a parameter of typeIEnumerable(Of TypeOfItem)
.正如已批准的答案中所建议的,这有效。此处显示的工作示例:
As suggested in the approved answer, this worked. A workinge example shown here: