PC Review


Reply
Thread Tools Rate Thread

Generic BindingList.SumItems.MyProperty: howto?

 
 
Pieter
Guest
Posts: n/a
 
      15th Feb 2007
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


 
Reply With Quote
 
 
 
 
=?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=
Guest
Posts: n/a
 
      15th Feb 2007
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.

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com


"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
>
>
>

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Just checking: BindingList<T> is the only generic list supportingchange notifications? Steve K Microsoft C# .NET 1 8th Apr 2008 09:46 AM
BindingList.OrderBy() - HowTo herbert Microsoft Dot NET 0 20th Jan 2008 04:45 PM
Generic BindingList.SumItems.MyProperty: howto? Pieter Microsoft Dot NET 1 15th Feb 2007 03:58 PM
Generic BindingList.SumItems.MyProperty: howto? Pieter Microsoft VB .NET 1 15th Feb 2007 03:58 PM
quick howto- howto disable tftp.exe anyweb Windows XP Security 0 13th Aug 2003 11:06 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:56 PM.