473,394 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Generic BindingList.SumItems.MyProperty: howto?

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
Feb 15 '07 #1
1 1448
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
Feb 15 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kevin | last post by:
If I had the following variables defined: System.Collections.Generic.BindingList<String> obj1; System.Collections.Generic.BindingList<Int32> obj2; And then I wanted to tell if an object was...
1
by: Pieter | last post by:
Hi, I have my own custom BindingList, that inherits from BindingList, and implements IComponent. I would like to implement a Find-method, that will allow me to search for a property of my...
1
by: Dave Booker | last post by:
Is there a reason why the BindingList constructor doesn't create a new BindingList? Create a form with four ListBoxes and the following will result in them all containing the exact same thing. ...
25
by: Lars | last post by:
Hi, I have a base class holding a generic list that needs to be accessed by both the base class and its subclasses. What is the best solution to this? I am fairly new to generics, but I am...
2
by: Angel Mateos | last post by:
I have this structure: Class ElemBase Class Elem1 : Inherits ElemBase Class ColecBase(Of GenElem As {ElemBase, New}) : Inherits System.ComponentModel.BindingList(Of GenElem) Class Colec1...
1
by: Pieter | last post by:
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....
3
by: cowznofsky | last post by:
I'm getting data in a generic list class, which I'm not going to change. I would like to use it as a datasource for a datagrid or a gridview, but it doesn't implement IEnumerable. I'm...
4
by: tadmill | last post by:
Hi, Is it possible for a generic list class to use Reflection on the generic type being used to detect a property within that type that refers to the same generic class, only with a different...
0
by: Andrus | last post by:
Who is going to raise the events? Events are raised by DataGridView overridden OnCellValidated method. However code uses row entity as sender because: 1. Code can use sender. without casting...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.