473,785 Members | 2,772 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.Control s 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:

DoStuffWithCont rols(((Control) validControl).C ontrols);
rather than
DoStuffWithCont rols(validContr ol.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 1365
Andrew Ducker <an****@ducker. org.ukwrote:
Let's say I have an interface (IValid) I want to add to all the
different Winform.Control s 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:

DoStuffWithCont rols(((Control) validControl).C ontrols);
rather than
DoStuffWithCont rols(validContr ol.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.co m>
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(ISometh ing 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
{
ValidImplementa tion<ValidTextB oxv;

public ValidTextBox()
{
v = new ValidImplementa tion<ValidTextB ox>(this);
}

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

public ValidImplementa tion(T control)
{
this.control = control;
}

#region IValid Members
public void ValidateMe()
{
foreach (Control c in control.Control s)
{
//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
2356
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 oriented design (owreilly - Juval Lowy), and it was actually very nice. The book goes on about how we should use Interfaces exposure instead of classes (this is my terminology and english is not my language so I hope you understand what I'm on about...).
5
13945
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 book and article I seem to run across jumps right into "and here's how Interfaces are implemented..." followed by a block of meaningless (to me) code without explaining why this is being used in the first place. e.g. Why not use a Class object? ...
21
2727
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 foo (id, b) values (3, 2); ERROR: b_is_fkey referential integrity violation - key referenced from
1
1049
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 fields vary from 50 to 150 fields. Based on the data collected from all the vendors, my application should consolidate all the inputs and assess the value / criticality / complexity of each product based on the business rules. Also it has to provide...
22
2116
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 a class, that when implemented, they require the implementing calss to make sure that each of the properties, functions etc. are handled by the class. (????) What I am really having a problem with is how they overcome the limitation
18
2391
by: Tony | last post by:
class Interface { public: virtual void DoItNow()=0; }; class A: public Interface { public: void DoItNow(); // satisfies interface explicitly
10
2003
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 implementation seems pretty flexible and straightforward. Thanks.
26
1592
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 Iv1Impl : public Iv1 { virtual method0(){...} }
23
2224
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
10319
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10087
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8971
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.