473,785 Members | 2,794 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interface and enums

Bob
Hi,
Can someone explain why you can't declare enums in an interface?
The compiler says "interfaces can't declare types"
Ignoring the syntax implications it seems to me that you should be able to
declare that an enum exists. i.e. It is within the 'spirit' of an interface
to state that it exists. The implementing class will provide the members.

e.g. I have a code library that performs a function.
It exposes two interfaces which allow for two different ways of asking for
the function.
One way involves a public enum, i.e. Some of the method calls accept the
enum as a parameter, the other does not.
It seems to me that I should be able to declare the enum in one interface
and leave it out of the other.
Currently the enum is available to declared objects of either interface.
e.g.
ImyFirstInterfa ce FirstInterface = new MyLib.myImpleme ntingclass();
ImySecondInterf ace SecondInterface = new MyLib.myImpleme ntingclass();

Both FirstInterface and SecondInterface can see MyLib.myPublicE num
thanks
Bob


Feb 7 '07 #1
13 5014
>Ignoring the syntax implications it seems to me that you should be able to
declare that an enum exists. i.e. It is within the 'spirit' of an interface
to state that it exists. The implementing class will provide the members.
How would your client code consume the enum if you don't know its
members?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 7 '07 #2
Bob
Hi Mattias,
I see it happening in much the same way as any other interface
implementation.
i.e.

****Interface code *****
public Interface IMyInterface
{
enum MyJamChoice{};
bool JamInStock(MyJa mChoice whichone);
}
****End Interface Code

*****Implementa tion code*****
In MyImplementatio nClass we have
enum ImyInterface.My JamChoice
{
Strawberry,
Apricot
}
bool ImyInterface.Ja mInStock(ImyInt erface.MyJamCho ice whichone)
{
//Jam checking code return true if in stock
}
**********End of Implementation code *******

****Client Code***
ImyInterface = new MyImplementatio nClass()
//at this point Intellisense knows the enum contents
if (ImyInterface.J amInStock(ImyIn terface.MyJamCh oice.StrawBerry )
{//Lets have breakfast}
**** End of Client code****
regards
Bob
"Mattias Sjögren" <ma************ ********@mvps.o rgwrote in message
news:e6******** ******@TK2MSFTN GP02.phx.gbl...
>
Ignoring the syntax implications it seems to me that you should be able
to
declare that an enum exists. i.e. It is within the 'spirit' of an
interface
to state that it exists. The implementing class will provide the members.

How would your client code consume the enum if you don't know its
members?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Feb 8 '07 #3
>****Client Code***
>ImyInterface = new MyImplementatio nClass()
Should I assume that ImyInterface is a variable of type IMyInterface
or what?
>//at this point Intellisense knows the enum contents
if (ImyInterface.J amInStock(ImyIn terface.MyJamCh oice.StrawBerry )
It does? If ImyInterface is indeed a variable of type IMyInterface,
the compiler knows nothing about the concrete implementation
(MyImplementati onClass) you're using. That's kind of the point of
using interfaces - to be able to write the same code regardless of
implementation.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 8 '07 #4
Hi Bob,

Bob napisa³(a):
Hi Mattias,
I see it happening in much the same way as any other interface
implementation.
i.e.

****Interface code *****
public Interface IMyInterface
{
enum MyJamChoice{};
bool JamInStock(MyJa mChoice whichone);
}
****End Interface Code

*****Implementa tion code*****
In MyImplementatio nClass we have
enum ImyInterface.My JamChoice
{
Strawberry,
Apricot
}
bool ImyInterface.Ja mInStock(ImyInt erface.MyJamCho ice whichone)
{
//Jam checking code return true if in stock
}
**********End of Implementation code *******
You can not just define the interface of (any) type!
Interface is the contract wich gives all needed functionality.
If you declare the empty interface then the only thing
you can provide is the "empty" implementation of it.
The value types can implement interfaces but the enums can not,
because they have only values as a members.

It is important that if you want to provide the interface
with enums (as a arguments), you'll have to define these
enums, because you need to define the contract.

With regards
Marcin
Feb 8 '07 #5
bob
Hi Marcin and Mattias,
I know that I can't do this.
This is more a philisophical discussion on why not.
I see nothing wrong with defining an empty enum in the interface and
providing the members in the implementation.
I have yet to be convinced that it should not be achievable.
my pseudo code was meant to convey this.
Harking back to my Jam stocktake.
What is wrong with the concept?
I would implement the interface and fill the enum with Strawberry and
apricot.
You could implement the interface and fill the enum with
Plum and blackberry.
My client code could choose one of my jams
Your client code could choose one of your jams.
regards
Bob
"Marcin Grzêbski" <mg************ ***@taxussi.com .plwrote in message
news:eq******** **@news.superme dia.pl...
Hi Bob,

Bob napisa³(a):
Hi Mattias,
I see it happening in much the same way as any other interface
implementation.
i.e.

****Interface code *****
public Interface IMyInterface
{
enum MyJamChoice{};
bool JamInStock(MyJa mChoice whichone);
}
****End Interface Code

*****Implementa tion code*****
In MyImplementatio nClass we have
enum ImyInterface.My JamChoice
{
Strawberry,
Apricot
}
bool ImyInterface.Ja mInStock(ImyInt erface.MyJamCho ice whichone)
{
//Jam checking code return true if in stock
}
**********End of Implementation code *******

You can not just define the interface of (any) type!
Interface is the contract wich gives all needed functionality.
If you declare the empty interface then the only thing
you can provide is the "empty" implementation of it.
The value types can implement interfaces but the enums can not,
because they have only values as a members.

It is important that if you want to provide the interface
with enums (as a arguments), you'll have to define these
enums, because you need to define the contract.

With regards
Marcin

Feb 8 '07 #6
PS
"Bob" <bo*@nowhere.co mwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
Hi Mattias,
I see it happening in much the same way as any other interface
implementation.
i.e.

****Interface code *****
public Interface IMyInterface
{
enum MyJamChoice{};
bool JamInStock(MyJa mChoice whichone);
}
****End Interface Code

*****Implementa tion code*****
In MyImplementatio nClass we have
enum ImyInterface.My JamChoice
{
Strawberry,
Apricot
}
bool ImyInterface.Ja mInStock(ImyInt erface.MyJamCho ice whichone)
{
//Jam checking code return true if in stock
}
**********End of Implementation code *******

****Client Code***
ImyInterface = new MyImplementatio nClass()
//at this point Intellisense knows the enum contents
if (ImyInterface.J amInStock(ImyIn terface.MyJamCh oice.StrawBerry )
{//Lets have breakfast}
**** End of Client code****
Enums are not "dynamic" like that.

Here is an example to see if this is the problem you are trying to avoid.

interface ICarFactory
{
Car MakeCar(ModelsE num model);
}

ICarFactory factory = new FordFactory();
factory.MakeCar (ModelsEnum.Toy otaRav4);

PS
regards
Bob
"Mattias Sjögren" <ma************ ********@mvps.o rgwrote in message
news:e6******** ******@TK2MSFTN GP02.phx.gbl...
>>
>Ignoring the syntax implications it seems to me that you should be able
to
>declare that an enum exists. i.e. It is within the 'spirit' of an
interface
>to state that it exists. The implementing class will provide the
members.

How would your client code consume the enum if you don't know its
members?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Feb 8 '07 #7
On Feb 8, 3:20 am, "bob" <b...@nowhere.c omwrote:
Hi Marcin and Mattias,
I know that I can't do this.
This is more a philisophical discussion on why not.
I see nothing wrong with defining an empty enum in the interface and
providing the members in the implementation.
What it two classes that implement your interface have two different
implementations for the enum? What if they then need to communicate
the enum variable to a third class? How is the third class to know
which implementation of the enum to use? What if they conflict?

Feb 8 '07 #8
bob
Hi,
Your example is much more succint than mine.
Excellent.
In my brave new world your ICarFactory interface would also declare
ModelEnum{}
Your FordFactory implemenation would be forced to fill it. Hopefully with
'starlet','esco rt' etc

In the client code
Once the ; is typed in
ICarFactory factory = new FordFactory();
Intellisense knows the contents of ModelEnum from its inspection of the
FordFactory implementation
When you type in
factory.MakeCar (ModelsEnum.
You are presented with the contents of Fordfactory.Mod elsEnum
Rav4 would not be in the list and would force a type checking error if you
insisted on typing it in.
regards
Bob
Enums are not "dynamic" like that.

Here is an example to see if this is the problem you are trying to avoid.

interface ICarFactory
{
Car MakeCar(ModelsE num model);
}

ICarFactory factory = new FordFactory();
factory.MakeCar (ModelsEnum.Toy otaRav4);

PS
regards
Bob
"Mattias Sjögren" <ma************ ********@mvps.o rgwrote in message
news:e6******** ******@TK2MSFTN GP02.phx.gbl...
>
Ignoring the syntax implications it seems to me that you should be
able
to
declare that an enum exists. i.e. It is within the 'spirit' of an
interface
to state that it exists. The implementing class will provide the
members.

How would your client code consume the enum if you don't know its
members?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Feb 8 '07 #9
bob
Hi Chris,
Please see my reply to PS. His example is much better than mine.
There can be no confusion because at some point you must instantiate your
interface declaration in the client code. Once you type in
IMyInterface m = new SomeImplementat ion();
You have locked down the enum to what ever is in the SomeImplementat ion.
regards
Bob
"Chris Dunaway" <du******@gmail .comwrote in message
news:11******** *************@q 2g2000cwa.googl egroups.com...
On Feb 8, 3:20 am, "bob" <b...@nowhere.c omwrote:
Hi Marcin and Mattias,
I know that I can't do this.
This is more a philisophical discussion on why not.
I see nothing wrong with defining an empty enum in the interface and
providing the members in the implementation.

What it two classes that implement your interface have two different
implementations for the enum? What if they then need to communicate
the enum variable to a third class? How is the third class to know
which implementation of the enum to use? What if they conflict?

Feb 8 '07 #10

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

Similar topics

13
9556
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could change and lead to memory corruption. I didn't see how this was possible. He claims that if a dll built for a program is built with different compiler settings than the launcher program, the enum size could change. The only way I could figure...
2
2082
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't seem to find an appropriate Reflection method to access Enums within a class I would like to loop through the Enums in the 'Foo' Class retrieve the Enums 'Car' and 'House Public Class Fo Public Enum Ca For Chevrole Toyot
8
3772
by: Ash | last post by:
Hello all, I am hoping this is the appropriate newsgroup for a C++ interface design question. I am trying to design an interface for a subscriber to register/deregister handlers for various events. The callbacks specified by the subscriber will be called when the events get trigerred in a different thread. Each event has different kinds of data associated with it. To achieve this I have the following: // The following describes the...
5
2828
by: Peter Berry | last post by:
I was surprised and somewhat disappointed by the fact that you cannot declare types in an interface. I discovered this when defining an interface that included events, where the definition of the delegates had to be placed outside the definition of the interface. To me, the delegate definition is an integral part of the interface definition. Also, I could imagine cases where one would like to define enums and structs in interfaces as well (to be...
0
1068
by: Chua Wen Ching | last post by:
Hi there, I am wondering when i had a class that extends an interface (currently in my interface i only had methods), but should i also extend Struct, Enum or even P/Invoke methods? If it can, how do i code struct or enum or p/invoke in an interface??? just say my struct is:
7
14785
by: _DD | last post by:
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?
1
1869
by: oldVB3r | last post by:
I would like to create a set of web services that share a common set of types (classes, structures, enums). I know I can create a VS 2005 web service based on the Interface statement to contain these common types. The problem is that I cannot determine how to create the implementation web services in a way that I can cast the common types between proxy object parameters in a client app. Please check the snippets of my code: 'Interface...
3
3757
by: Andrew Bainbridge | last post by:
Hi, I'm converting some code over from vb.net to c# and have the following issue. In VB you can have enum's within an interface. i.e. Public Interface Class1 Enum ColorType Black Red
1
3762
by: Cartoper | last post by:
I am working on a project that currently has some interfaces and enums defined in an IDL file. The sole implementor of these interfaces are now in C++ CLI, but I still have consumers that are in unmanaged C++. I would like to move the interface and enum definitions to the C++ CLI code, but I cannot figure out what shape it should take: typedef enum { associate, chief,
0
9645
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
9480
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,...
1
10092
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
8974
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...
1
7500
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
6740
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
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.