472,959 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,959 software developers and data experts.

Generics with multiple constrains

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 type constraint 'System.Windows.Forms.ComboBox' must come before
any other constraints

If I switch place of ComboBox with TextBoxBase, it would gripe:

The class type constraint 'System.Windows.Forms.TextBoxBase' must come
before any other constraints

Is it possible to create such a generics class? Tks.
Sep 9 '07 #1
6 5344
You can specify multiple constraints, but in your example there must be only
one base class (since C# doesn't allow multiple inheritance) and the
following constraints could indicate interfaces.
e.g.,
public class KeyHandler<Twhere T : TextBoxBase, ISomeInterface

For your example, the compiler should actually be providing a more
intelligent message, such as "T can only derive from one base class".
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
"Quan Nguyen" wrote:
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 type constraint 'System.Windows.Forms.ComboBox' must come before
any other constraints

If I switch place of ComboBox with TextBoxBase, it would gripe:

The class type constraint 'System.Windows.Forms.TextBoxBase' must come
before any other constraints

Is it possible to create such a generics class? Tks.

Sep 9 '07 #2
Thanks for your response. In my case, I want it specifically for TextBox and
ComboBox. Their base class Control, however, does not have the common
methods/properties that they share, such as Select and SelectText.

I guess generics may not be applied to my specific example.

"David Anton" wrote:
You can specify multiple constraints, but in your example there must be only
one base class (since C# doesn't allow multiple inheritance) and the
following constraints could indicate interfaces.
e.g.,
public class KeyHandler<Twhere T : TextBoxBase, ISomeInterface

For your example, the compiler should actually be providing a more
intelligent message, such as "T can only derive from one base class".
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
"Quan Nguyen" wrote:
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 type constraint 'System.Windows.Forms.ComboBox' must come before
any other constraints

If I switch place of ComboBox with TextBoxBase, it would gripe:

The class type constraint 'System.Windows.Forms.TextBoxBase' must come
before any other constraints

Is it possible to create such a generics class? Tks.
Sep 9 '07 #3
So you would want to specify a constraint list where just one of the items is
true. The constraint list items must all be true, so you could never do this
in C#.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
"Quan Nguyen" wrote:
Thanks for your response. In my case, I want it specifically for TextBox and
ComboBox. Their base class Control, however, does not have the common
methods/properties that they share, such as Select and SelectText.

I guess generics may not be applied to my specific example.

"David Anton" wrote:
You can specify multiple constraints, but in your example there must be only
one base class (since C# doesn't allow multiple inheritance) and the
following constraints could indicate interfaces.
e.g.,
public class KeyHandler<Twhere T : TextBoxBase, ISomeInterface

For your example, the compiler should actually be providing a more
intelligent message, such as "T can only derive from one base class".
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
"Quan Nguyen" wrote:
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 type constraint 'System.Windows.Forms.ComboBox' must come before
any other constraints
>
If I switch place of ComboBox with TextBoxBase, it would gripe:
>
The class type constraint 'System.Windows.Forms.TextBoxBase' must come
before any other constraints
>
Is it possible to create such a generics class? Tks.
>
>
Sep 9 '07 #4
Well, I wouldn't say that you can't do it in C#. If you are willing to
trade compile-time checking for run-time checking (which is not the best
solution, admittedly), then you can have no constraint and then perform the
check to make sure that the type parameter is a ComboBox or TextBox in the
static constructor. If it isn't, then you can throw an exception.

Also, you could define a common interface which has these properties,
and then implement these interfaces on wrappers that would take instances of
either TextBox or ComboBox and then use the interface in the class you are
writing (you won't need generics, since you are only ever dealing with the
interface).

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

"David Anton" <Da********@discussions.microsoft.comwrote in message
news:DA**********************************@microsof t.com...
So you would want to specify a constraint list where just one of the items
is
true. The constraint list items must all be true, so you could never do
this
in C#.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
"Quan Nguyen" wrote:
>Thanks for your response. In my case, I want it specifically for TextBox
and
ComboBox. Their base class Control, however, does not have the common
methods/properties that they share, such as Select and SelectText.

I guess generics may not be applied to my specific example.

"David Anton" wrote:
You can specify multiple constraints, but in your example there must be
only
one base class (since C# doesn't allow multiple inheritance) and the
following constraints could indicate interfaces.
e.g.,
public class KeyHandler<Twhere T : TextBoxBase, ISomeInterface

For your example, the compiler should actually be providing a more
intelligent message, such as "T can only derive from one base class".
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
"Quan Nguyen" wrote:

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 type constraint 'System.Windows.Forms.ComboBox' must come
before
any other constraints

If I switch place of ComboBox with TextBoxBase, it would gripe:

The class type constraint 'System.Windows.Forms.TextBoxBase' must
come
before any other constraints

Is it possible to create such a generics class? Tks.

Sep 9 '07 #5
Right - nearly anything can be done with creative coding, but I was just
looking at this very narrowly in terms of generic constraint lists.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
"Nicholas Paldino [.NET/C# MVP]" wrote:
Well, I wouldn't say that you can't do it in C#. If you are willing to
trade compile-time checking for run-time checking (which is not the best
solution, admittedly), then you can have no constraint and then perform the
check to make sure that the type parameter is a ComboBox or TextBox in the
static constructor. If it isn't, then you can throw an exception.

Also, you could define a common interface which has these properties,
and then implement these interfaces on wrappers that would take instances of
either TextBox or ComboBox and then use the interface in the class you are
writing (you won't need generics, since you are only ever dealing with the
interface).

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

"David Anton" <Da********@discussions.microsoft.comwrote in message
news:DA**********************************@microsof t.com...
So you would want to specify a constraint list where just one of the items
is
true. The constraint list items must all be true, so you could never do
this
in C#.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
"Quan Nguyen" wrote:
Thanks for your response. In my case, I want it specifically for TextBox
and
ComboBox. Their base class Control, however, does not have the common
methods/properties that they share, such as Select and SelectText.

I guess generics may not be applied to my specific example.

"David Anton" wrote:

You can specify multiple constraints, but in your example there must be
only
one base class (since C# doesn't allow multiple inheritance) and the
following constraints could indicate interfaces.
e.g.,
public class KeyHandler<Twhere T : TextBoxBase, ISomeInterface

For your example, the compiler should actually be providing a more
intelligent message, such as "T can only derive from one base class".
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
"Quan Nguyen" wrote:

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 type constraint 'System.Windows.Forms.ComboBox' must come
before
any other constraints
>
If I switch place of ComboBox with TextBoxBase, it would gripe:
>
The class type constraint 'System.Windows.Forms.TextBoxBase' must
come
before any other constraints
>
Is it possible to create such a generics class? Tks.
>
>
Sep 9 '07 #6
It seems that I can't use generics in my example, even though I want to,
since this is my first foray into generics stuff.

I took Nic's suggestions and developed a wrapper class that implements an
interface that features the common properties/methods of the two Windows
Forms controls. It works nicely.

Thanks for your inputs. Have a great day.

"David Anton" wrote:
Right - nearly anything can be done with creative coding, but I was just
looking at this very narrowly in terms of generic constraint lists.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
"Nicholas Paldino [.NET/C# MVP]" wrote:
Well, I wouldn't say that you can't do it in C#. If you are willing to
trade compile-time checking for run-time checking (which is not the best
solution, admittedly), then you can have no constraint and then perform the
check to make sure that the type parameter is a ComboBox or TextBox in the
static constructor. If it isn't, then you can throw an exception.

Also, you could define a common interface which has these properties,
and then implement these interfaces on wrappers that would take instances of
either TextBox or ComboBox and then use the interface in the class you are
writing (you won't need generics, since you are only ever dealing with the
interface).

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

"David Anton" <Da********@discussions.microsoft.comwrote in message
news:DA**********************************@microsof t.com...
So you would want to specify a constraint list where just one of the items
is
true. The constraint list items must all be true, so you could never do
this
in C#.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
>
>
"Quan Nguyen" wrote:
>
>Thanks for your response. In my case, I want it specifically for TextBox
>and
>ComboBox. Their base class Control, however, does not have the common
>methods/properties that they share, such as Select and SelectText.
>>
>I guess generics may not be applied to my specific example.
>>
>"David Anton" wrote:
>>
You can specify multiple constraints, but in your example there must be
only
one base class (since C# doesn't allow multiple inheritance) and the
following constraints could indicate interfaces.
e.g.,
public class KeyHandler<Twhere T : TextBoxBase, ISomeInterface
>
For your example, the compiler should actually be providing a more
intelligent message, such as "T can only derive from one base class".
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
>
>
"Quan Nguyen" wrote:
>
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 type constraint 'System.Windows.Forms.ComboBox' must come
before
any other constraints
>
If I switch place of ComboBox with TextBoxBase, it would gripe:
>
The class type constraint 'System.Windows.Forms.TextBoxBase' must
come
before any other constraints
>
Is it possible to create such a generics class? Tks.
>
>
Sep 9 '07 #7

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

Similar topics

3
by: Eddie | last post by:
Hi, I've recently read the articles on generics in the .net framework (the next version) and just wanted to check a few points.. As I understand, generic class instances while being strongly...
11
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...
27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still cant use generics... Im trying to make a generic collection myCollection<vartype> and...
1
by: Shmulik | last post by:
Is there a way to constrain a Generics class to only accept specific value types, for instance, lets say I want to create a Histogram class that could accept the types int, float, double, etc.,...
3
by: Marshal | last post by:
/////////////////////////////////////////////////////////////////////////////////////////////// /// CONSTRAINTS ON GENERICS //////////////////////////////////////////////////// public class...
8
by: Dave Booker | last post by:
Make a template of a template: public class A<T> { A(string s){} } public class D<Tobject, Tclass> : where Tobject : Object
19
by: Barry Mossman | last post by:
Hi, if I have a generic class such as: public class MyGroup<T> : Collection<MyChildClass> and from one of it's methods I want to pass a reference to myself to the following method in another...
8
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem...
0
by: tolcis | last post by:
Hi! I need to know the proper way to split existing databases into multiple file groups. How do I move existing tables into different file groups (keeping all constrains intact) and move indexes...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.