473,796 Members | 2,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interface and enum

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
Yellow
White
End Enum
End Interface
You don't seem to be able to do this with c#.

i.e.
namespace ClassLibrary1
{
public interface Class1
{
public enum ColorType { Black, Red, Yellow, White };
}
}

This fails with:
Error 1 'ColorType': interfaces cannot declare types
D:\Dev\test\Cla ssLibrary1\Clas s1.cs 9 17 ClassLibrary1
It is valid in VB.Net to do this. If this can't be done where is the best
place to store the enum? Could you just bump it into the namespace? i.e.
namespace ClassLibrary1
{
public enum ColorType { Black, Red, Yellow, White };
public interface Class1
{
}
}
Just trying to understand what is best practice with enums and interfaces
under c#.

Would be good if someone who has an understanding of how the compiler
converts the VB.Net code to IL so I could implement it in a similar manner
in C#.
T.I.A.
Dec 18 '06 #1
3 3758
Looking at the IL, it is pretty-much as per the VB... it just looks
like you can't do this directly in C# (similar to SomeRandomMetho dName
implementing ISomeInterface. AnotherMethodNa me, which you can do in
VB.Net but not *directly* (i.e. as the IL level) via C#).

My personal view is that the next level up (typically namespace) is
fine for this scenario.

Marc
Dec 18 '06 #2
Marc Gravell wrote:
Looking at the IL, it is pretty-much as per the VB... it just looks
like you can't do this directly in C# (similar to SomeRandomMetho dName
implementing ISomeInterface. AnotherMethodNa me, which you can do in
VB.Net but not *directly* (i.e. as the IL level) via C#).

My personal view is that the next level up (typically namespace) is
fine for this scenario.

Marc

I usually make sure that nested types (classes/structs/enums) are
protected or private most of the time (there are some exceptions), and
in that perspective there's no use to include these kind of types in an
interface.

Best Regards,

Wiebe Tijsma
Dec 18 '06 #3
Hi,

No, you cannot do that in C#, promoting it to the namespace is the only way
to do it.
--
Ignacio Machin
machin AT laceupsolutions com

"Andrew Bainbridge" <so*****@micros oft.comwrote in message
news:OA******** ******@TK2MSFTN GP03.phx.gbl...
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
Yellow
White
End Enum
End Interface
You don't seem to be able to do this with c#.

i.e.
namespace ClassLibrary1
{
public interface Class1
{
public enum ColorType { Black, Red, Yellow, White };
}
}

This fails with:
Error 1 'ColorType': interfaces cannot declare types
D:\Dev\test\Cla ssLibrary1\Clas s1.cs 9 17 ClassLibrary1
It is valid in VB.Net to do this. If this can't be done where is the best
place to store the enum? Could you just bump it into the namespace? i.e.
namespace ClassLibrary1
{
public enum ColorType { Black, Red, Yellow, White };
public interface Class1
{
}
}
Just trying to understand what is best practice with enums and interfaces
under c#.

Would be good if someone who has an understanding of how the compiler
converts the VB.Net code to IL so I could implement it in a similar manner
in C#.
T.I.A.


Dec 18 '06 #4

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...
1
16704
by: Torben Madsen | last post by:
I need a intereface so that i get the same structure in several arrays that i create. I think that i need to use enum in a interface. Abstract classes don't do the job as i need to inherit from several interfaces. This doesn't Work. Any ideas? using System; namespace well_test { public interface IMeassurement
2
5949
by: David | last post by:
hello... how can i use IActiveDesktop COM interface from shell32.dll? thanx...
2
2843
by: matthew_glen_evans | last post by:
Hi there, Quick one about interfaces in c#. It seems that it is illegal to declare types within an interface. I was quite used to doing this in VB.net where the interface can define a clients remoting interface and object model. eg..
0
1425
by: Robin Tucker | last post by:
I need to create my own explorer-style control - I don't seem to have access to IShellFolder - assuming I need it (which I think I do), how can this be done? I've imported shell32.dll into my references. I've seen a C# version of how to do it, but I have no idea how to translate this into VB :-0. (C# version below, no idea if it works or not!)
2
20147
by: Cristiano de Pádua Milagres Oliveira | last post by:
What do I to declare the header of a Property in an Interface, and should this property return a generic Enum? Example: public interface IMetric { Enum Unit { get;} } public class ClassTest : IMetric
13
5016
by: Bob | last post by:
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...
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
905
by: habakkuk | last post by:
Hi, I gotten Protected Memory Error while trying to access the C++ interface from C#, I am wandering is it something is not correct on my interpretation from C++ to C#. Below is the C++ Interface code and next to it is my C# code. //*************** C++ Code ***************************************************** // {AEB23DA0-54BB-11d2-BD98-0080ADB67301} DEFINE_GUID(IID_IAudioOutput, 0xaeb23da0, 0x54bb, 0x11d2, 0xbd, 0x98, 0x0, 0x80, 0xad,...
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...
1
10169
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
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
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?
1
4115
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 we have to send another system
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.