Generics passing Class type

G

Guest

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 <T> then I could pass
in a <T> generic 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?
 
M

Marc Gravell

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
 
P

Peter Huang [MSFT]

Hi Steve,

Here is a good start for your reference.
Introducing Generics in the CLR
http://msdn.microsoft.com/msdnmag/issues/06/00/NET/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.
 

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