473,471 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Attach constants to an interface

_DD
I've encountered situations that call for an interface but also
require enums/constants that go hand-in-hand with the interface.
Abstract classes won't do, cause I may need to inherit more than one.
Any elegant solutions come to mind?

May 15 '06 #1
7 14701
kh
Perhaps I've misunderstood your question, but is this not something that can
be achieved with a property defined on the interface with only a getter (i.e.
a read-only property)? Obviously you cannot define values within the
interface definition, but in this way you can require your implementors to
supply one...

interface IMyInterface
{
string Name { get; }
}

class MyClass : IMyInterface
{
public string Name
{
get { return "Foo"; }
}
}

May 15 '06 #2
_DD <_D*@nospam.com> wrote:
I've encountered situations that call for an interface but also
require enums/constants that go hand-in-hand with the interface.
Abstract classes won't do, cause I may need to inherit more than one.
Any elegant solutions come to mind?


Just use a separate enum type. Just because two concepts are related
doesn't mean they have to be part of the same type.

--
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
May 15 '06 #3
_DD
On Mon, 15 May 2006 00:01:02 -0700, kh <kh@newsgroups.nospam> wrote:
Perhaps I've misunderstood your question, but is this not something that can
be achieved with a property defined on the interface with only a getter (i.e.
a read-only property)? Obviously you cannot define values within the
interface definition, but in this way you can require your implementors to
supply one...


I was thinking of simple constants. For instance, if an interface had
a function: PaintBackgroundPurple(), it would make sense to have a
defined Purple that could travel with the interface. In other words,
the implementation of how to paint the background would be variable,
but you don't want the particular shade of Purple to vary. Everyone
that implements the interface would use the same Purple.

In that sense, it would be nice to have tighter coupling between the
Interface and the defined colors that it is using.

This may not be the perfect example, but I do run into variants on the
scenario once in a while.

I understand that allowing constants within the Interface could result
in diamond inheritance, with multiple Purple defines converging when
multiple interfaces are inherited, but I was wondering if there is
another way to implement this.

May 15 '06 #4
_DD <_D*@nospam.com> wrote:
On Mon, 15 May 2006 00:01:02 -0700, kh <kh@newsgroups.nospam> wrote:
Perhaps I've misunderstood your question, but is this not something that can
be achieved with a property defined on the interface with only a getter (i.e.
a read-only property)? Obviously you cannot define values within the
interface definition, but in this way you can require your implementors to
supply one...


I was thinking of simple constants. For instance, if an interface had
a function: PaintBackgroundPurple(), it would make sense to have a
defined Purple that could travel with the interface. In other words,
the implementation of how to paint the background would be variable,
but you don't want the particular shade of Purple to vary. Everyone
that implements the interface would use the same Purple.

In that sense, it would be nice to have tighter coupling between the
Interface and the defined colors that it is using.


It seems more sensible to have PaintBackground(PredefinedColor) and
have a PredefinedColor enum to go alongside the interface.

I just don't see the advantage of having the constants *in* the
interface rather than as a separate type to go alongside it.

--
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
May 16 '06 #5
Sorry I don't know a way of introducing simple constants within interfaces
You might be able to get an equivalent *look-and-feel* through nested
namespaces.

I think that being able to introduce constants into interfaces is very
important since some interfaces have a real-world analogy.

eg. // Contrived example

interface IRS232_Physical
{
const int MIN_LINE_VOLTAGE = -12;
const int MAX_LINE_VOLTAGE = 12;

void public ToggleRTS(bool TurnOn);
void public TransmitHoldingReg();

// ...
}

class USBToRS232Converter: IWin32SerialAPI, IUSBStack, IRS232_Physical
{
public void TransmitHoldingReg()
{
// Get TX Holding Reg

// Send Start Bits

// Send Data Bits

// Send Stop Bits
}
}

"_DD" wrote:
I've encountered situations that call for an interface but also
require enums/constants that go hand-in-hand with the interface.
Abstract classes won't do, cause I may need to inherit more than one.
Any elegant solutions come to mind?

Jul 3 '06 #6
steve <st***@discussions.microsoft.comwrote:
I think that being able to introduce constants into interfaces is very
important since some interfaces have a real-world analogy.

eg. // Contrived example

interface IRS232_Physical
{
const int MIN_LINE_VOLTAGE = -12;
const int MAX_LINE_VOLTAGE = 12;

void public ToggleRTS(bool TurnOn);
void public TransmitHoldingReg();

// ...
This looks awfully like the Java Constant Interface Antipattern
(Effective Java (http://java.sun.com/docs/books/effective/) Item 17).

-- Barry

--
http://barrkel.blogspot.com/
Jul 3 '06 #7
The Java cons... what??? Me not speaka da language ; )

Never heard of it sorry. I'll put it in the to have a look at basket.

Ciao,

Steve

"Barry Kelly" wrote:
steve <st***@discussions.microsoft.comwrote:
I think that being able to introduce constants into interfaces is very
important since some interfaces have a real-world analogy.

eg. // Contrived example

interface IRS232_Physical
{
const int MIN_LINE_VOLTAGE = -12;
const int MAX_LINE_VOLTAGE = 12;

void public ToggleRTS(bool TurnOn);
void public TransmitHoldingReg();

// ...

This looks awfully like the Java Constant Interface Antipattern
(Effective Java (http://java.sun.com/docs/books/effective/) Item 17).

-- Barry

--
http://barrkel.blogspot.com/
Jul 4 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Sandeep Sharma | last post by:
Right from the time the first edition of K&R was released, the advantages of using symbolic constants, as opposed to "magic numbers", has been emphasized ---- and for good reason. I don't dispute...
0
by: David W. Fenton | last post by:
Today I was working on a hideous old app that I created a long time ago that does a lot of showing/hiding/resizing of fields on one of the forms. I had used constants to store reference values for...
4
by: Julie J. | last post by:
When working w/ forms and controls, in 95% of the cases, the value of the control is used somewhere in the program (duh!). However, there doesn't seem to be an easy way to attach a value variable...
3
by: Lenonardo | last post by:
Can anyone tell me how I refer to Excel constants when using late binding. I've tried searching the Excel.application interface but can't find any reference in the hierarchy to an object that...
7
by: Iain Mcleod | last post by:
Hi This must be an often encountered problem. I want to declare an abstract class or an interface with nothing but several static constants so that I can use polymorphism when I call each of them...
9
by: Jon Davis | last post by:
I am working with an application that is compiled as a COM EXE (written in Delphi 7). I don't know if it's the nature of COM EXE or if it's implemented in the COM EXE, but when I create a new...
6
by: PC | last post by:
Gentlesofts, Forgive me. I'm an abject newbie in your world, using VB 2005 with the dot-Net wonderfulness. So, I'm writing a wonderful class or two to interface with a solemnly ancient...
3
by: boris | last post by:
Hi. If somebody can help out with this design question, thank you very much. Suppose I have a global constant, like an images directory. This constant will be referenced from multiple classes. How...
12
by: Gordon | last post by:
I want to provide a set of static functions in a superclass that work with class constants defined in a decendant of that class. Unfortunately I've run into a snag with this idea. Example: ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.