473,947 Members | 34,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generic BindingList.Sum Items.MyPropert y: 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.S umItems("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.S umItems.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 PropertyDescrip torCollection
Dim myProperty As PropertyDescrip tor
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(M e.Items(0).GetT ype)
myProperty = properties.Find (ColumnName, True)

If myProperty Is Nothing Then
Throw New ArgumentExcepti on("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 1514
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.S umItems("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.S umItems.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 PropertyDescrip torCollection
Dim myProperty As PropertyDescrip tor
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(M e.Items(0).GetT ype)
myProperty = properties.Find (ColumnName, True)

If myProperty Is Nothing Then
Throw New ArgumentExcepti on("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
2345
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 any type of generic BindingList what would the syntax be? I can get it to work with non-generic types, but can't seem to get the statement to return back true for both obj1 and obj2. Something like the following doesn't work:
1
8938
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 Items. So basicly: If my Items in the List have a property Name, I would like to be able to return with this method all the items that have Name = 'Bill Gates' etc.
1
3054
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. The expected behavior would be for each of the first three to contain different sets numbers. public Form1() { InitializeComponent();
25
3059
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 aware of that fact that if you have a class B, that inherits from A, then List<Bdoes NOT inherit from List<A>. So I understand why the example below does not compile, but I fail to
2
1499
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 : Inherits ColecBase(Of Elem1)
1
1890
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. One of them is for instance SumItems, which returns me the sum of a given Property of all the Items in the List.
3
13946
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 wondering if there's a simple technique that would allow me to get the data into the datagrid.
4
2306
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 type as the parameter? From various posts and help, this is what I've got so far: public class DataList<T: BindingList<T>
0
209
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 2. Allow to use same pattern for Textbox OnValidated call 3. Allow to use type inference for TRow. Probably caller can simply use OnRowCellValidated( rowEntity, ...
0
9982
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11573
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11346
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8254
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7429
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6113
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4945
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4537
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3542
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.