Interface Constraints

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

Hey all...

Anyone know if it is possible to apply a constraint to an interface
itself? I'm not talking about a class having a constraint but rather
an interface having a constraint.

Thanks
 
Pete,

I did try some things in code but can't seem to find anything that
would "hint" to me it would work.

I can't say if the constraint I am talking about is "generic" or
not... I am looking for something similar however...

I would like to have an interface that can only be inherited and not
usable as a parameter or declaration... Not sure this is possible but
thought I would ask...

Allow This:

public class SomeClass : ISomeContract

Do Not Allow These:

public ISomeContract DoSomeWork(...) {...}
public ISomeContract Value { get; set; }
public void DoSomeWork(ISomeContract contract) {...}

Make sense? I understand what an interface is for... just wondering if
there is a way to limit the use to inheritance only...
 
I know all that...

Thought we could use these forums for discussions but apparently we
can't think outside the box... stick to the rules damnit!!!

Anyway,

What about an interface only allowed to attach to a specific class?
This was the point of what I originally wanted but lost my train of
thought...

Let's use simple college here:

public class Vehicle : IVehicle{}

public class Car : Vehicle{}

public class Boat : Vehicle{}

public class Dog : Animal, IVehicle{} ** OH NO!!

If we make an interface called IVehicle... can we make sure it can
only be attached to a "Vehicle" class?
 
Anyway,

What about an interface only allowed to attach to a specific class?
This was the point of what I originally wanted but lost my train of
thought...

Let's use simple college here:

public class Vehicle : IVehicle{}

public class Car : Vehicle{}

public class Boat : Vehicle{}

public class Dog : Animal, IVehicle{} ** OH NO!!

If we make an interface called IVehicle... can we make sure it can
only be attached to a "Vehicle" class?

What exactly would be the advantage and/or difference of using an
interface instead of an abstract class here?

Following the thread until your last post, it's still not clear to me
what the problem is you're trying to solve with your suggestion. Maybe
if you can describe that more clearly, more appropriate suggestions are
possible.
 
What exactly would be the advantage and/or difference of using an
interface instead of an abstract class here?

Following the thread until your last post, it's still not clear to me
what the problem is you're trying to solve with your suggestion. Maybe
if you can describe that more clearly, more appropriate suggestions are
possible.

Not a problem

I have a control library where each control inherits a specific
class... Like MyButton inheriting Button... MyListControl inheriting
ListControl... etc

As we know...each of these derive from Control. I would like an
interface that will only attach to Control types... The interface
implements specific behavior that is only for these types and I would
like to avoid anyone trying to implement the interface on a WebControl
let's say.

So that is the question... can we create an interface that is only
allowed on a specific class type?
 
No.  And in general, if you are trying to design code with that
requirement, your design is broken.

Pete

Not designed that way... it came up in within the team but I wanted to
see what other people thought and if it was even possible. The
conclusion was the same as your post... why would you... and of course
the fact that no one could find a way to do it... but maybe we just
don't "know" how...
 
Mark said:
I know all that...

Thought we could use these forums for discussions but apparently we
can't think outside the box... stick to the rules damnit!!!

Apparently to you there's no place in a discussion for explanations of
why something can't be done or is a bad idea or doesn't make sense.

If I tell you that I don't eat breakfast standing on my head, are you
going to accuse me of not thinking outside the box?
Anyway,

What about an interface only allowed to attach to a specific class?
This was the point of what I originally wanted but lost my train of
thought...

Let's use simple college here:

public class Vehicle : IVehicle{}

public class Car : Vehicle{}

public class Boat : Vehicle{}

public class Dog : Animal, IVehicle{} ** OH NO!!

If we make an interface called IVehicle... can we make sure it can
only be attached to a "Vehicle" class?

No, and if you wanted to do that, then why in the world would you have
defined the IVehicle interface in the first place? That's like declaring
variables that you have no intention of using.
 
Back
Top