Re: Generics with multiple constrains
- From: David Anton <DavidAnton@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 8 Sep 2007 20:12:01 -0700
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]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
"David Anton" <DavidAnton@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:DA93D8ED-CF4B-47F4-BF00-85C188E5F962@xxxxxxxxxxxxxxxx
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<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".
--
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<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.
- Follow-Ups:
- Re: Generics with multiple constrains
- From: Quan Nguyen
- Re: Generics with multiple constrains
- References:
- RE: Generics with multiple constrains
- From: David Anton
- Re: Generics with multiple constrains
- From: Nicholas Paldino [.NET/C# MVP]
- RE: Generics with multiple constrains
- Prev by Date: Re: Generics with multiple constrains
- Next by Date: How to stop a thread
- Previous by thread: Re: Generics with multiple constrains
- Next by thread: Re: Generics with multiple constrains
- Index(es):
Relevant Pages
|