473,545 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ 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<Obj ectlist
list = (BindingList<Ob ject>) gridview.DataSo urce
Object row = list[5]

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

Suggestions?
jeff
Jul 26 '07 #1
4 2842
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.co m

"Jeff Jarrell" <jj************ @yahoo.comwrote in message
news:%2******** ********@TK2MSF TNGP02.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<Obj ectlist
list = (BindingList<Ob ject>) gridview.DataSo urce
Object row = list[5]

The casts don't work because they are BindingList<Con creteType>. 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<Obj ectlist
list = (BindingList<Ob ject>) gridview.DataSo urce
Object row = list[5]

The casts don't work because they are BindingList<Con creteType>. 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.Data Source)[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<Obj ectlist
list = (BindingList<Ob ject>) gridview.DataSo urce
Object row = list[5]

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

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

--
Jon Skeet - <sk***@pobox.co m>
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.c omwrote in
message news:1D******** *************** ***********@mic rosoft.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.co m

"Jeff Jarrell" <jj************ @yahoo.comwrote in message
news:%2******** ********@TK2MSF TNGP02.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<Obj ectlist
list = (BindingList<Ob ject>) gridview.DataSo urce
Object row = list[5]

The casts don't work because they are BindingList<Con creteType>. 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
3975
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 looked at the files in the Gyro download but I couldn't find any mention of constraints. Can anyone enlighten me what the current status is and what we...
13
1769
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 hashtable that supports (or implements) generics? I haven't tried generics hands-on, but I would assume that once you don't have to do casting anymore,...
17
3295
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 way they are. ***** Summary & Questions ***** In a nutshell, the current .NET generics & .NET framework make it sometimes difficult or even...
3
1946
by: Marshal | last post by:
/////////////////////////////////////////////////////////////////////////////////////////////// /// CONSTRAINTS ON GENERICS //////////////////////////////////////////////////// public class Node<T> where T:IComparable<T> I don't like the syntax, and would prefer something that groups the constraint along with the type that it governs...
5
1151
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 Foo(Of DerivedBase) Dim zzz As Foo(Of Base)
11
2478
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 provide an implemation of the operator +. Now I want to write another generic class Plan<DType>, which can
7
3243
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 System.Collections.Generic namespace. Literature I have read on this ties generics in with collections, hence articulate their examples as such. That's fine,...
18
2374
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 generic module, it is something that can be used exactly as it is. The generic H-file and/or C-file, should not have to be edited AT ALL in order...
13
3800
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 application that needs implementations in both Java and C#. I have the Java side done, and it works fantastic, and the C# side is nearly there. The...
0
7499
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7689
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. ...
0
7943
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6022
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5359
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...
0
5076
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...
0
3490
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...
1
1919
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
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.