如何返回 TYPED 结果?

发布于 2024-11-01 21:28:10 字数 1255 浏览 0 评论 0原文

我想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜未央樱花落 2024-11-08 21:28:10

您还可以使用 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# 中的代码示例。

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#.

顾忌 2024-11-08 21:28:10

唯一的方法是这样做:

Dim Sorted As ErrorProviderMessageCollection = 
    New ErrorProviderMessageCollection(From item In unsortedCollection 
                                       Order By item.Control.TabIndex)

为此,ErrorProviderMessageCollection 需要一个采用 IEnumerable(Of TypeOfItem) 类型参数的构造函数。

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).

梦初启 2024-11-08 21:28:10

正如已批准的答案中所建议的,这有效。此处显示的工作示例:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文