473,395 Members | 1,658 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,395 software developers and data experts.

Generics passing Class type

I'm currently creating a custom column in a DataGridView. I'm wondering if
Generics can be used to solve an issue I'm seeing.

When creating the editing control for the column cell I have the code:

class MyEditingControl : MyTextBox, IDataGridViewEditingControl
{ ... } (The control is currently working just fine.)
The column class is defined like this:
class MySpecialColumn : DataGridViewColumn
{ ... }

If I could cause "MyTextBox" to be seen as a generic <Tthen I could pass
in a <Tgeneric to the column when it is created and added to the
DataGridView. This would allow me to add multiple columns but using
different "cell" types in my code. If this is possible can someone show me
how the code should look when defining such a thing?

I'm looking for something like this for the control:
class MyEditingControl<T: T, IDataGridViewEditingControl
{ ... }

I'm looking for something like this when defining the column to be placed
into the DataGridView.
class MySpecialColumn<T where T : Class : DataGridViewColumn
{...}

If this is possible does anyone know how?
--
-----------
Thanks,
Steve
Aug 17 '06 #1
3 1185
So no, this (generic inheritance) is illegal:
class MyEditingControl<T: T, IDataGridViewEditingControl
{ ... }
However you could do this:
class MyEditingControl<T: IDataGridViewEditingControl where T :
Control
{
private T Control; // either pass in via ctor, or declare T: new()
and create
}

i.e. you can *contain* a T which you can then display in the UI, and
manipulate (using only, by default, the properties on the known
base-class, i.e. those of Control in this case.

Marc

Aug 18 '06 #2
I'll read up more on generics to understand it better. Thanks.
--
-----------
Thanks,
Steve
"Marc Gravell" wrote:
So no, this (generic inheritance) is illegal:
class MyEditingControl<T: T, IDataGridViewEditingControl
{ ... }
However you could do this:
class MyEditingControl<T: IDataGridViewEditingControl where T :
Control
{
private T Control; // either pass in via ctor, or declare T: new()
and create
}

i.e. you can *contain* a T which you can then display in the UI, and
manipulate (using only, by default, the properties on the known
base-class, i.e. those of Control in this case.

Marc

Aug 21 '06 #3
Hi Steve,

Here is a good start for your reference.
Introducing Generics in the CLR
http://msdn.microsoft.com/msdnmag/is...T/default.aspx

If you have any concern, please feel free to post here.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 22 '06 #4

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

Similar topics

8
by: Leicester B. Ford Jr. | last post by:
I have come across a problem using generics. I want to create a Factory type that will build classes for me. Code snippet below... static public class Product<T> where T : new() { static...
3
by: Chris | last post by:
Hi, I'm porting a fairly hefty n-tier implementation from .Net 1.1 -> .Net 2.0 (and fixing a few things in the process). One of the improvements I am making is moving the existing non-generic...
13
by: Luc Vaillant | last post by:
I try to compare to values of generic value type T in a generic class as follow: public class C<T> where T : struct { private T value1; private T value2; C(T value1, T value2) {
11
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...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
13
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...
6
by: =?Utf-8?B?UXVhbiBOZ3V5ZW4=?= | last post by:
I am trying to create a generics class with multiple constrains, as follows: public class KeyHandler<Twhere T : TextBoxBase, ComboBox When I try that, the compiler would complain: The class...
3
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I have some problems with Crystal Reports (Version 10.2, Runtime 2.0). In Section3 I have added a OLE Object (Bitmap). Now when I open the report in my code I would like to set this...
3
by: Anders Borum | last post by:
Hello, I've worked on an API for quite some time and have (on several occasions) tried to introduce generics at the core abstract level of business objects (especially a hierarchical node). The...
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: 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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.