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? 7 14786
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"; }
}
}
_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
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.
_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
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?
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/
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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...
|
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
|
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.
|
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...
| |
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...
|
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.
|
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...
|
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 ()
{
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |