473,796 Members | 2,826 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
13 5016
>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
No it doesn't. IntelliSense looks at the type of the variable. It
doesn't know about the actual implementing type used at runtime. Your
example is too simplistic. When using interfaces, it's very common to
not know the actual type that will be used.

Take a look at this for example

void Foo(bool preferAmericanC ars)
{
ICarFactory factory;
if (preferAmerican Car)
factory = new FordFactory();
else
factory = new ToyotaFactory() ;

factory.MakeCar (...

At this point, which "implementation " of the enum would you use? And
how would IntelliSense and the compiler know?
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 #11
Bob
Hi Mattias,
Yep,
that is a weakness with my idea given the current semantics.

I guess what I am proposing here would need to be some sort of 'Sealed
Interface'
i.e. You can only declare say a 'IFordFactory' or a 'IToyotaFactory '

My real world situation that led to this line of reasoning is:
AppA has an internal widgetprocessin g class
AppB also processes widgets but does it using a DLL because it is
fundamentally different to AppA
Along comes a third party SuperWidgetProc essor class library that I want to
bolt on to AppA and also make available to AppB users who may or may not
decide to take it on board.

So I wrap SuperWidgetProc essor and expose its functionality through two
interfaces say IA and IB.
Easiest way to insert into AppA is to have the AppA WidgetProcessin g class
declare a private IA and route all its functionality to it. This way all
changes are confined to the WidgetProcessin g class.

Trouble is that the other classes tell the WidgetProcessin g class what they
want by passing in a request class that has Properties based on enums. So my
wrapper exposes a set of identically named enums and the routing code is
readable.
AppB uses a completely different approach so the enum thing is not needed.

So..
I am left with a wrapper that has a public enum that is useful to only one
out of two interfaces.
Hence my current line of reasoning of wanting to expose the enum only
through IA.

Ah well,
Perhaps in another life.
Thank you all for your thoughts.
regards
Bob

"Mattias Sjögren" <ma************ ********@mvps.o rgwrote in message
news:OU******** ******@TK2MSFTN GP04.phx.gbl...
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

No it doesn't. IntelliSense looks at the type of the variable. It
doesn't know about the actual implementing type used at runtime. Your
example is too simplistic. When using interfaces, it's very common to
not know the actual type that will be used.

Take a look at this for example

void Foo(bool preferAmericanC ars)
{
ICarFactory factory;
if (preferAmerican Car)
factory = new FordFactory();
else
factory = new ToyotaFactory() ;

factory.MakeCar (...

At this point, which "implementation " of the enum would you use? And
how would IntelliSense and the compiler know?
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 #12
PS

"Bob" <bo*@nowhere.co mwrote in message
news:uw******** ******@TK2MSFTN GP03.phx.gbl...
Hi Mattias,
Yep,
that is a weakness with my idea given the current semantics.

I guess what I am proposing here would need to be some sort of 'Sealed
Interface'
i.e. You can only declare say a 'IFordFactory' or a 'IToyotaFactory '

My real world situation that led to this line of reasoning is:
AppA has an internal widgetprocessin g class
AppB also processes widgets but does it using a DLL because it is
fundamentally different to AppA
Along comes a third party SuperWidgetProc essor class library that I want
to
bolt on to AppA and also make available to AppB users who may or may not
decide to take it on board.

So I wrap SuperWidgetProc essor and expose its functionality through two
interfaces say IA and IB.
Easiest way to insert into AppA is to have the AppA WidgetProcessin g class
declare a private IA and route all its functionality to it. This way all
changes are confined to the WidgetProcessin g class.

Trouble is that the other classes tell the WidgetProcessin g class what
they
want by passing in a request class that has Properties based on enums. So
my
wrapper exposes a set of identically named enums and the routing code is
readable.
AppB uses a completely different approach so the enum thing is not needed.

So..
I am left with a wrapper that has a public enum that is useful to only one
out of two interfaces.
Hence my current line of reasoning of wanting to expose the enum only
through IA.
I did get a little lost in your explanation. But if you want to keep "type
safety" with your enums then you can cast between different enums by
converting them to integers temporarily.

For example

enum CarModel
{
FordEscort = 1,
FordEscape = 2,
ToyotaRav4 = 3
}

enum FordModel
{
FordEscort = 1,
FordEscape = 2
}

enum ToyotaModel
{
ToyotaRav4 = 3
}

This way the FordFactory takes FordModel, the ToyotaFactory takes
ToyotaModel and your wrapper / abstract factory takes CarModel. The wrapper
/ abstract factory does need to do validate the values but after they are
validated they are converted like (ToyotaModel)(i nt)CarModel.Toy otaRav4.

Maybe this will help?

PS
>


"Mattias Sjögren" <ma************ ********@mvps.o rgwrote in message
news:OU******** ******@TK2MSFTN GP04.phx.gbl...
>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

No it doesn't. IntelliSense looks at the type of the variable. It
doesn't know about the actual implementing type used at runtime. Your
example is too simplistic. When using interfaces, it's very common to
not know the actual type that will be used.

Take a look at this for example

void Foo(bool preferAmericanC ars)
{
ICarFactory factory;
if (preferAmerican Car)
factory = new FordFactory();
else
factory = new ToyotaFactory() ;

factory.MakeCar (...

At this point, which "implementation " of the enum would you use? And
how would IntelliSense and the compiler know?
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 9 '07 #13
bob
Hi PS,
I think my problem is that I didn't look at the big picture properly.
I got locked into trying to hide the enum from InterfaceB consumers. But
when I look downstream clearly the InterfaceB offering will be presented to
the consumers as a new version of their existng DLL so they won't see any of
the InterfaceA baggage anyway.
regards
Bob

<ec***********@ hotmail.comwrot e in message
news:Oq******** ********@TK2MSF TNGP02.phx.gbl. ..
>
"Bob" <bo*@nowhere.co mwrote in message
news:uw******** ******@TK2MSFTN GP03.phx.gbl...
Hi Mattias,
Yep,
that is a weakness with my idea given the current semantics.

I guess what I am proposing here would need to be some sort of 'Sealed
Interface'
i.e. You can only declare say a 'IFordFactory' or a 'IToyotaFactory '

My real world situation that led to this line of reasoning is:
AppA has an internal widgetprocessin g class
AppB also processes widgets but does it using a DLL because it is
fundamentally different to AppA
Along comes a third party SuperWidgetProc essor class library that I want
to
bolt on to AppA and also make available to AppB users who may or may
not
decide to take it on board.

So I wrap SuperWidgetProc essor and expose its functionality through two
interfaces say IA and IB.
Easiest way to insert into AppA is to have the AppA WidgetProcessin g
class
declare a private IA and route all its functionality to it. This way all
changes are confined to the WidgetProcessin g class.

Trouble is that the other classes tell the WidgetProcessin g class what
they
want by passing in a request class that has Properties based on enums.
So
my
wrapper exposes a set of identically named enums and the routing code is
readable.
AppB uses a completely different approach so the enum thing is not
needed.

So..
I am left with a wrapper that has a public enum that is useful to only
one
out of two interfaces.
Hence my current line of reasoning of wanting to expose the enum only
through IA.

I did get a little lost in your explanation. But if you want to keep "type
safety" with your enums then you can cast between different enums by
converting them to integers temporarily.

For example

enum CarModel
{
FordEscort = 1,
FordEscape = 2,
ToyotaRav4 = 3
}

enum FordModel
{
FordEscort = 1,
FordEscape = 2
}

enum ToyotaModel
{
ToyotaRav4 = 3
}

This way the FordFactory takes FordModel, the ToyotaFactory takes
ToyotaModel and your wrapper / abstract factory takes CarModel. The
wrapper
/ abstract factory does need to do validate the values but after they are
validated they are converted like (ToyotaModel)(i nt)CarModel.Toy otaRav4.

Maybe this will help?

PS



"Mattias Sjögren" <ma************ ********@mvps.o rgwrote in message
news:OU******** ******@TK2MSFTN GP04.phx.gbl...
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

No it doesn't. IntelliSense looks at the type of the variable. It
doesn't know about the actual implementing type used at runtime. Your
example is too simplistic. When using interfaces, it's very common to
not know the actual type that will be used.

Take a look at this for example

void Foo(bool preferAmericanC ars)
{
ICarFactory factory;
if (preferAmerican Car)
factory = new FordFactory();
else
factory = new ToyotaFactory() ;

factory.MakeCar (...

At this point, which "implementation " of the enum would you use? And
how would IntelliSense and the compiler know?
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 9 '07 #14

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

Similar topics

13
9558
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
2083
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
3774
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
14786
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
3758
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
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...
0
9050
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...
0
6785
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
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...
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.