Generics and general purpose helpers

J

Jeff Jarrell

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

I have been using BindingList<T> to 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<Object> list
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
 
N

Nicholas Paldino [.NET/C# MVP]

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).
 
J

John B

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

I have been using BindingList<T> to 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<Object> list
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
 
J

Jon Skeet [C# MVP]

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

I have been using BindingList<T> to 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<Object> list
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<T> implements IList - could you just cast to that instead?
 
J

Jeff Jarrell

That did the trick. Thanks.

Nicholas Paldino said:
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]
- (e-mail address removed)

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

I have been using BindingList<T> to 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<Object> list
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top