473,796 Members | 2,826 Online
Bytes | Software Development & Data Engineering Community
+ 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 14786
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.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
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: PaintBackground Purple(), 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: PaintBackground Purple(), 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 (PredefinedColo r) 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.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
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_VOLTAG E = -12;
const int MAX_LINE_VOLTAG E = 12;

void public ToggleRTS(bool TurnOn);
void public TransmitHolding Reg();

// ...
}

class USBToRS232Conve rter: IWin32SerialAPI , IUSBStack, IRS232_Physical
{
public void TransmitHolding Reg()
{
// 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***@discussi ons.microsoft.c omwrote:
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_VOLTAG E = -12;
const int MAX_LINE_VOLTAG E = 12;

void public ToggleRTS(bool TurnOn);
void public TransmitHolding Reg();

// ...
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***@discussi ons.microsoft.c omwrote:
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_VOLTAG E = -12;
const int MAX_LINE_VOLTAG E = 12;

void public ToggleRTS(bool TurnOn);
void public TransmitHolding Reg();

// ...

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
2113
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 that at all. However, it gets on my nerves when people carry this practice too far. Consider these examples (from the code written by a distinguished colleague): #define DASH '-' #define SLASH '/' #define SINGLE_BYTE 1
0
3067
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 many of the top/height/left settings. I noticed that some of the constants were defined by using other constant values, and I was impressed that VBA could do that. Here's an example: Const row1Top = 1.0208 * 1440 ' top of first row Const...
4
2067
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 to that control the automatically handles synchronization of the value w/ the contol. Here is an example: // some form
3
2555
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 holds constants (such as XlPattern.XL.....). This must be possible (e.g. as with a typelib) but I can't track it down. Any help appreciated.
7
9299
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 from my code. My stab at the problem is shown below. Can anyone suggest what my most efficient workable solution would be (i.e. I don't want to have to create instances of the classes as they will only store constant information and I would...
9
1915
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 object based on the COM interface using C#, it references the first process/instance it finds of this interface. This is not ideal for me; I would like to create a modal dialog box that makes the user select which process to "attach" to. Getting a list...
6
3125
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 database. In times recently past, I would have done this with Borland Delphi. So, that's my perspective and I have my old Delphi code to crib from. That seems good.
3
1806
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 do I define this constant only once while using OOP? I could define the constant on one of the classes and then reference it with ClassName::const from the other classes, but this ties the constants to one class arbitrarily. In Java you can...
12
6144
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: class SuperClass { const CNST = 'Super class'; public static function getCnst () {
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9525
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10452
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...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7546
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
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
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
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.