Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / February 2007

Tip: Looking for answers? Try searching our database.

Generic BindingList.SumItems.MyProperty: howto?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Pieter - 15 Feb 2007 08:29 GMT
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
Ciaran O''Donnell - 15 Feb 2007 14:58 GMT
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.

Signature

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

> Hi,
>
[quoted text clipped - 65 lines]
>         Return decSum
>     End Function

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.