473,480 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Generics and general purpose helpers

I am working with Winforms and writing some general purpose helpers for use
with a 3rd party grid (devexpress).

I have been using BindingList<Tto bind to the grid. At some point I need
to get the object stored at the Nth index of the list.

The intent is something like the following...
BindingList<Objectlist
list = (BindingList<Object>) gridview.DataSource
Object row = list[5]

The casts don't work because they are BindingList<ConcreteType>. I don't
know how to make the < ... part dynamic. And no, I can't use DataSets.

Suggestions?
jeff
Jul 26 '07 #1
4 2841
Jeff,

Cast the DataSource to IList (the non-generic version) and then access
it through that interface's indexer. You will get an object back, but I
assume that's fine, since you don't seem to know the type anyways (or can't
make assumptions about it in your code).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jeff Jarrell" <jj************@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>I am working with Winforms and writing some general purpose helpers for use
with a 3rd party grid (devexpress).

I have been using BindingList<Tto bind to the grid. At some point I
need to get the object stored at the Nth index of the list.

The intent is something like the following...
BindingList<Objectlist
list = (BindingList<Object>) gridview.DataSource
Object row = list[5]

The casts don't work because they are BindingList<ConcreteType>. I don't
know how to make the < ... part dynamic. And no, I can't use DataSets.

Suggestions?
jeff

Jul 26 '07 #2
Jeff Jarrell wrote:
I am working with Winforms and writing some general purpose helpers for use
with a 3rd party grid (devexpress).

I have been using BindingList<Tto bind to the grid. At some point I need
to get the object stored at the Nth index of the list.

The intent is something like the following...
BindingList<Objectlist
list = (BindingList<Object>) gridview.DataSource
Object row = list[5]

The casts don't work because they are BindingList<ConcreteType>. I don't
know how to make the < ... part dynamic. And no, I can't use DataSets.

Suggestions?
jeff

public T ItemByIndex<T>(int index)
{
return ((BindingList<T>)gridview.DataSource)[index];
}

Or Nicholas's way :)

JB
Jul 26 '07 #3
Jeff Jarrell <jj************@yahoo.comwrote:
I am working with Winforms and writing some general purpose helpers for use
with a 3rd party grid (devexpress).

I have been using BindingList<Tto bind to the grid. At some point I need
to get the object stored at the Nth index of the list.

The intent is something like the following...
BindingList<Objectlist
list = (BindingList<Object>) gridview.DataSource
Object row = list[5]

The casts don't work because they are BindingList<ConcreteType>. I don't
know how to make the < ... part dynamic. And no, I can't use DataSets.

Suggestions?
BindingList<Timplements IList - could you just cast to that instead?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 26 '07 #4
That did the trick. Thanks.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:1D**********************************@microsof t.com...
Jeff,

Cast the DataSource to IList (the non-generic version) and then access
it through that interface's indexer. You will get an object back, but I
assume that's fine, since you don't seem to know the type anyways (or
can't make assumptions about it in your code).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jeff Jarrell" <jj************@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>>I am working with Winforms and writing some general purpose helpers for
use with a 3rd party grid (devexpress).

I have been using BindingList<Tto bind to the grid. At some point I
need to get the object stored at the Nth index of the list.

The intent is something like the following...
BindingList<Objectlist
list = (BindingList<Object>) gridview.DataSource
Object row = list[5]

The casts don't work because they are BindingList<ConcreteType>. I
don't know how to make the < ... part dynamic. And no, I can't use
DataSets.

Suggestions?
jeff


Jul 26 '07 #5

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

Similar topics

11
3958
by: andrew queisser | last post by:
I've read some material on the upcoming Generics for C#. I've seen two types of syntax used for constraints: - direct specification of the interface in the angle brackets - where clauses I...
13
1759
by: Anders Borum | last post by:
Hello! Now that generics are introduces with the next version of C#, I was wondering what kind of performance gains we're going to see, when switching from e.g. the general hashtable to a...
17
3290
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
3
1933
by: Marshal | last post by:
/////////////////////////////////////////////////////////////////////////////////////////////// /// CONSTRAINTS ON GENERICS //////////////////////////////////////////////////// public class...
5
1148
by: PeterLawrance | last post by:
Hi, I would expect that inheritance through the parameters of a generic class would be fine however I get errors as below in the following code. Public Class Form1 Sub test() Dim xxx As...
11
2463
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes...
7
3236
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
18
2371
by: ttl_idiot | last post by:
This post is about how to get generic modules in a C environment. The aim is to get close to Ada generics. Using C++ is not an option. It should all run in a simple C compiler. When I think of a...
13
3787
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
0
7034
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,...
1
6732
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...
0
5324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4768
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...
0
4472
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...
0
2990
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...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
174
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...

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.