By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,896 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,896 IT Pros & Developers. It's quick & easy.

Suggestion - interfaces and constraints

P: n/a
Let's say I have an interface (IValid) I want to add to all the
different Winform.Controls classes (textbox, radiobutton, etc). And I
have a bunch of methods that then use these IValid Controls. Sometimes
they access them as Controls, sometimes as IValids. Which means
casting back and forth. You end up with things like:

DoStuffWithControls(((Control)validControl).Contro ls);
rather than
DoStuffWithControls(validControl.Controls);

and I know which one I prefer.

Having thought about how Generics use Constraints, it occured to me
that with a simple addition of inheritance constraints you could say
that a IValid can only be implemented by a Control, and therefore it
would automatically have all the methods/properties of both IValid
_and_ Control.

No casting would be necessary and the code would be cleaner/simpler.

Any thoughts?

Andy

Sep 25 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Andrew Ducker <an****@ducker.org.ukwrote:
Let's say I have an interface (IValid) I want to add to all the
different Winform.Controls classes (textbox, radiobutton, etc). And I
have a bunch of methods that then use these IValid Controls. Sometimes
they access them as Controls, sometimes as IValids. Which means
casting back and forth. You end up with things like:

DoStuffWithControls(((Control)validControl).Contro ls);
rather than
DoStuffWithControls(validControl.Controls);

and I know which one I prefer.

Having thought about how Generics use Constraints, it occured to me
that with a simple addition of inheritance constraints you could say
that a IValid can only be implemented by a Control, and therefore it
would automatically have all the methods/properties of both IValid
_and_ Control.

No casting would be necessary and the code would be cleaner/simpler.

Any thoughts?
Do you mean like this?

using System;

interface ISomething
{
void Foo();
}

class Base
{
public void Bar()
{
}
}

class Derived : Base, ISomething
{
public void Foo()
{
}
}

class Test
{
static void Main()
{
Derived d = new Derived();

UseBoth (d);
}

static void UseBoth<T(T instance) where T : Base, ISomething
{
instance.Foo();
instance.Bar();
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 25 '06 #2

P: n/a
Jon wrote:
Do you mean like this?

static void UseBoth<T(T instance) where T : Base, ISomething
{
instance.Foo();
instance.Bar();
}
}
Oooh, that is nice. But it requires the use of generics. What I'd
like is

class Bar
{
public void Bar
{
...
}
}

class Derived : Bar, ISomething
{
public void Foo()
{
....
}
}

interface ISomething : where Bar
{
void Foo();
}

Class Derived1

class Test
{
static void Main()
{
Derived d = new Derived();

UseBoth (d);
}
static void UseBoth(ISomething instance)
{
instance.Foo();
instance.Bar();
}
}

The "where" in the interface effectively saying that "You can't
implement this interface in anything other than a Bar." meaning that
ISomething automatically has all of the properties/methods of a Bar.

The advantage there is that I can have a private member variable of
type ISomething that I can use as a Bar without recasting.

Andy

Sep 25 '06 #3

P: n/a
Actually, scratch what I said just there - I worked out a way to do it:

interface IValid
{
void ValidateMe();
}

class ValidTextBox: TextBox, IValid
{
ValidImplementation<ValidTextBoxv;

public ValidTextBox()
{
v = new ValidImplementation<ValidTextBox>(this);
}

#region IValid Members
public void ValidateMe()
{
v.ValidateMe();
}
#endregion
}
class ValidImplementation<T: IValid where T: Control,IValid
{
T control;

public ValidImplementation(T control)
{
this.control = control;
}

#region IValid Members
public void ValidateMe()
{
foreach (Control c in control.Controls)
{
//Do Stuff
}
}
#endregion
}

Cheers for the pointer in your previous post - it cracked it for me!

Andy

Sep 25 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.