Home All Groups Group Topic Archive Search About

Generic BindingList.SumItems.MyProperty: howto?

Author
15 Feb 2007 8:29 AM
Pieter
Hi,

I use a customized Generic List (VB.NET 2.0) inherited from BindingList(Of
T).

I made some methods in this BindingList, to allow me to do some standard
functions on the BindingList-Items.
One of them is for instance SumItems, which returns me the sum of a given
Property of all the Items in the List.

e.g.: MyBindingList.SumItems("Price") returns me the sum of all the prices
of my articles in the list.

But I would like to be able to access directly the Property's of the T-class
of my BindingList. It woudl be much much nicer if I could directly type in
the code editor MyBindingList.SumItems.Price, and that I can, while coding,
see al the proeprty's of my T-class after typing
"MyBindingList.SumItems."...

Because I use Generics I somehow think this should be possible, although I
don't find how...

Anybody has any ideas? Any help would be really appreciated!

Thanks a lot in advance,

Pieter

PS: My current SumItems-method:

    Public Function SumItems(ByVal ColumnName As String) As Decimal
        Dim properties As PropertyDescriptorCollection
        Dim myProperty As PropertyDescriptor
        Dim decSum As Decimal = 0

        Try
            ' Specify search columns
            If (ColumnName Is Nothing) Then
                Return Nothing
            End If

            ' Get list to search
            Dim lItems As List(Of T) = Me.Items

            If Me.Count > 0 Then
                properties =
TypeDescriptor.GetProperties(Me.Items(0).GetType)
                myProperty = properties.Find(ColumnName, True)

                If myProperty Is Nothing Then
                    Throw New ArgumentException("Invalid Property Name.",
ColumnName)
                    Return 0
                    Exit Function
                End If

                ' Traverse list for value
                For Each it As T In lItems
                    'Dim value As String = CStr(myProperty.GetValue(item))
                    decSum += CSng(myProperty.GetValue(it))
                Next it
            End If

        Catch ex As Exception
            Throw ex
        End Try

        Return decSum
    End Function

Author
15 Feb 2007 2:58 PM
Ciaran O''Donnell
You would need to have the Price property declared in an Interface or base
class that you force T to implement or inherit from. Otherwise it isnt
possible.

Show quote
"Pieter" wrote:

> Hi,
>
> I use a customized Generic List (VB.NET 2.0) inherited from BindingList(Of
> T).
>
> I made some methods in this BindingList, to allow me to do some standard
> functions on the BindingList-Items.
> One of them is for instance SumItems, which returns me the sum of a given
> Property of all the Items in the List.
>
> e.g.: MyBindingList.SumItems("Price") returns me the sum of all the prices
> of my articles in the list.
>
> But I would like to be able to access directly the Property's of the T-class
> of my BindingList. It woudl be much much nicer if I could directly type in
> the code editor MyBindingList.SumItems.Price, and that I can, while coding,
> see al the proeprty's of my T-class after typing
> "MyBindingList.SumItems."...
>
> Because I use Generics I somehow think this should be possible, although I
> don't find how...
>
> Anybody has any ideas? Any help would be really appreciated!
>
> Thanks a lot in advance,
>
> Pieter
>
> PS: My current SumItems-method:
>
>     Public Function SumItems(ByVal ColumnName As String) As Decimal
>         Dim properties As PropertyDescriptorCollection
>         Dim myProperty As PropertyDescriptor
>         Dim decSum As Decimal = 0
>
>         Try
>             ' Specify search columns
>             If (ColumnName Is Nothing) Then
>                 Return Nothing
>             End If
>
>             ' Get list to search
>             Dim lItems As List(Of T) = Me.Items
>
>             If Me.Count > 0 Then
>                 properties =
> TypeDescriptor.GetProperties(Me.Items(0).GetType)
>                 myProperty = properties.Find(ColumnName, True)
>
>                 If myProperty Is Nothing Then
>                     Throw New ArgumentException("Invalid Property Name.",
> ColumnName)
>                     Return 0
>                     Exit Function
>                 End If
>
>                 ' Traverse list for value
>                 For Each it As T In lItems
>                     'Dim value As String = CStr(myProperty.GetValue(item))
>                     decSum += CSng(myProperty.GetValue(it))
>                 Next it
>             End If
>
>         Catch ex As Exception
>             Throw ex
>         End Try
>
>         Return decSum
>     End Function
>
>
>

AddThis Social Bookmark Button