Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Generics with multiple constrains

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Quan Nguyen - 09 Sep 2007 02:42 GMT
I am trying to create a generics class with multiple constrains, as follows:

   public class KeyHandler<T> where 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.
David Anton - 09 Sep 2007 02:58 GMT
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<T> where 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".
Signature

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

> I am trying to create a generics class with multiple constrains, as follows:
>
[quoted text clipped - 11 lines]
>
> Is it possible to create such a generics class? Tks.
Quan Nguyen - 09 Sep 2007 03:24 GMT
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.

> 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
[quoted text clipped - 20 lines]
> >
> > Is it possible to create such a generics class? Tks.
David Anton - 09 Sep 2007 03:32 GMT
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#.
Signature

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

> 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
[quoted text clipped - 26 lines]
> > >
> > > Is it possible to create such a generics class? Tks.
Nicholas Paldino [.NET/C# MVP] - 09 Sep 2007 04:05 GMT
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).

Signature

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

> So you would want to specify a constraint list where just one of the items
> is
[quoted text clipped - 37 lines]
>> > >
>> > > Is it possible to create such a generics class? Tks.
David Anton - 09 Sep 2007 04:12 GMT
Right - nearly anything can be done with creative coding, but I was just
looking at this very narrowly in terms of generic constraint lists.
Signature

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

>     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
[quoted text clipped - 49 lines]
> >> > >
> >> > > Is it possible to create such a generics class? Tks.
Quan Nguyen - 09 Sep 2007 21:24 GMT
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.

> Right - nearly anything can be done with creative coding, but I was just
> looking at this very narrowly in terms of generic constraint lists.
[quoted text clipped - 52 lines]
> > >> > >
> > >> > > Is it possible to create such a generics class? Tks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.