473,508 Members | 2,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Suggestion - interfaces and constraints

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
3 1349
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
2330
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
5
13931
by: dj | last post by:
I know seasoned OOP-savvy developers will roll their eyes at this one, but can anyone provide a clear explanation as to *why* one would need to use an Interface in a VB.NET application? Every...
21
2654
by: Karl O. Pinc | last post by:
FYI, It'd be nice if the error message from a REFERENCES constraint mentioned the column name into which the bad data was attempted to be inserted. In PostgreSQL 7.3: sandbox=> insert into...
1
1034
by: venkateshg | last post by:
Dear All, I request your suggestion for the following requirement. One of the requirements of my applications is to gather data from multiple vendors about their products - the number of...
22
2064
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to...
18
2371
by: Tony | last post by:
class Interface { public: virtual void DoItNow()=0; }; class A: public Interface { public: void DoItNow(); // satisfies interface explicitly
10
1978
by: hyperboreean | last post by:
Hi, Probably it has been asked before, but I'll still ask. Why doesn't python provide interfaces trough its standard library? Or it was ever proposed to be included in the language? Zope's...
26
1547
by: Chris Becke | last post by:
Given an interface (in the c++ sense, nothing more than a struct containing pure virtual methods) struct Iv1 { virtual method0()=0; }; And a class that implements the interface class...
23
2184
by: A.Gallus | last post by:
If I declare a function pure virtual: class A { virtual void myfunc() = 0; } and I derive a class from A: class B : public A
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7495
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.