How to represent an Enum in an Interface when you can't
Ok, so the basis of this post and to explain the title is simple. I have an Interface with a method. That method on the user side will take in an enum as a param. But you can't define enums in an interface therefore I don't see how I can even define this method then if I'm expecting a type Enum as one of the incoming params.
So how do you handle this situation? How can you still get that method in your Interface. You don't know what Enum they'll require to be sent in but you know for sure you want it to be an enum instead of magic strings.
An Enum is not a reference type so you can't use Object as the type for the incoming param. So not sure what to do here.
c# enums interface
add a comment |
Ok, so the basis of this post and to explain the title is simple. I have an Interface with a method. That method on the user side will take in an enum as a param. But you can't define enums in an interface therefore I don't see how I can even define this method then if I'm expecting a type Enum as one of the incoming params.
So how do you handle this situation? How can you still get that method in your Interface. You don't know what Enum they'll require to be sent in but you know for sure you want it to be an enum instead of magic strings.
An Enum is not a reference type so you can't use Object as the type for the incoming param. So not sure what to do here.
c# enums interface
add a comment |
Ok, so the basis of this post and to explain the title is simple. I have an Interface with a method. That method on the user side will take in an enum as a param. But you can't define enums in an interface therefore I don't see how I can even define this method then if I'm expecting a type Enum as one of the incoming params.
So how do you handle this situation? How can you still get that method in your Interface. You don't know what Enum they'll require to be sent in but you know for sure you want it to be an enum instead of magic strings.
An Enum is not a reference type so you can't use Object as the type for the incoming param. So not sure what to do here.
c# enums interface
Ok, so the basis of this post and to explain the title is simple. I have an Interface with a method. That method on the user side will take in an enum as a param. But you can't define enums in an interface therefore I don't see how I can even define this method then if I'm expecting a type Enum as one of the incoming params.
So how do you handle this situation? How can you still get that method in your Interface. You don't know what Enum they'll require to be sent in but you know for sure you want it to be an enum instead of magic strings.
An Enum is not a reference type so you can't use Object as the type for the incoming param. So not sure what to do here.
c# enums interface
c# enums interface
edited Oct 17 '18 at 20:29
Jim Fell
5,53527103183
5,53527103183
asked Jul 9 '10 at 20:30
PositiveGuyPositiveGuy
17.9k97262434
17.9k97262434
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
interface MyInterface
{
void MyMethod(Enum @enum);
}
3
what's the @ for? I tried to look it up in MSDN...sorry if this seems trivial. I'm new to creating interfaces.
– PositiveGuy
Jul 9 '10 at 20:34
8
anticipating your question:@enumis the name of the variable. There is nothing special about that syntax
– BlueRaja - Danny Pflughoeft
Jul 9 '10 at 20:34
22
@coffeeaddict: It allows you to use a reserved word for a parameter name. Without it, you'd get a compiler error (unless you rename "enum" to something else)
– Reed Copsey
Jul 9 '10 at 20:35
1
still Enum is a type declared in a different namespace even. The Enum type is yet declared outside an interface.
– Lucas Locatelli
Feb 23 '15 at 19:52
add a comment |
public enum MyEnum
{
Hurr,
Durr
}
public interface MyInterface
{
void MyMethod(MyEnum value);
}
If this isn't what you're talking about doing, leave a comment so people can understand what your issue is. Because, while the enum isn't defined within the interface, this is a completely normal and acceptable design.
4
I guess the question is if the method takes a specific enum, or ANY enum...
– Will
Jul 9 '10 at 20:40
+1 I created an iEventLogTypesclassas helper for my IEventLoginterface. Right now it only contains anenum, but I can add any "types" that I need within my interface. I like sticking the enum in a "similar named" class so that the enum is more tightly bound to the specific interface, even if it's in "name only".
– franji1
Oct 16 '15 at 15:12
add a comment |
Another solution could be to use Generic types:
public enum MyEnum
{
Foo,
Bar
}
public interface IDummy<EnumType>
{
void OneMethod(EnumType enumVar);
}
public class Dummy : IDummy<MyEnum>
{
public void OneMethod(MyEnum enumVar)
{
// Your code
}
}
Also, since C# 7.3, you can add a generic constraint to accept only Enum types:
public interface IDummy<EnumType> : where EnumType : Enum
{
void OneMethod(EnumType enumVar);
}
add a comment |
If you're talking about generic interfaces and the fact that C# doesn't let you constrain generic types to be enums, the answers to this question include two different work-arounds.
It does constrain, you just can't define the type inside the interface. The same way you do when you create an interface, add a namespace that contain a specific type then add that in the parameters or return types.
– Lucas Locatelli
Feb 23 '15 at 19:49
add a comment |
Defining an enum is like defining a class or defining an interface. You could just put it in one of your class files, inside the namespace but outside the class definition, but if several classes use it, which one do you put it in, and whichever you choose you will get "Type name does not match file name" warnings. So the "right" way to do it is to put it in its own file, as you would a class or an interface:
MyEnum.cs
namespace MyNamespace
{
internal enum MyEnum { Value1, Value2, Value3, Value4, Value5 };
}
Then any interfaces or classes within the namespace can access it.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3216340%2fhow-to-represent-an-enum-in-an-interface-when-you-cant%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
interface MyInterface
{
void MyMethod(Enum @enum);
}
3
what's the @ for? I tried to look it up in MSDN...sorry if this seems trivial. I'm new to creating interfaces.
– PositiveGuy
Jul 9 '10 at 20:34
8
anticipating your question:@enumis the name of the variable. There is nothing special about that syntax
– BlueRaja - Danny Pflughoeft
Jul 9 '10 at 20:34
22
@coffeeaddict: It allows you to use a reserved word for a parameter name. Without it, you'd get a compiler error (unless you rename "enum" to something else)
– Reed Copsey
Jul 9 '10 at 20:35
1
still Enum is a type declared in a different namespace even. The Enum type is yet declared outside an interface.
– Lucas Locatelli
Feb 23 '15 at 19:52
add a comment |
interface MyInterface
{
void MyMethod(Enum @enum);
}
3
what's the @ for? I tried to look it up in MSDN...sorry if this seems trivial. I'm new to creating interfaces.
– PositiveGuy
Jul 9 '10 at 20:34
8
anticipating your question:@enumis the name of the variable. There is nothing special about that syntax
– BlueRaja - Danny Pflughoeft
Jul 9 '10 at 20:34
22
@coffeeaddict: It allows you to use a reserved word for a parameter name. Without it, you'd get a compiler error (unless you rename "enum" to something else)
– Reed Copsey
Jul 9 '10 at 20:35
1
still Enum is a type declared in a different namespace even. The Enum type is yet declared outside an interface.
– Lucas Locatelli
Feb 23 '15 at 19:52
add a comment |
interface MyInterface
{
void MyMethod(Enum @enum);
}
interface MyInterface
{
void MyMethod(Enum @enum);
}
answered Jul 9 '10 at 20:32
Yuriy FaktorovichYuriy Faktorovich
53.5k1189124
53.5k1189124
3
what's the @ for? I tried to look it up in MSDN...sorry if this seems trivial. I'm new to creating interfaces.
– PositiveGuy
Jul 9 '10 at 20:34
8
anticipating your question:@enumis the name of the variable. There is nothing special about that syntax
– BlueRaja - Danny Pflughoeft
Jul 9 '10 at 20:34
22
@coffeeaddict: It allows you to use a reserved word for a parameter name. Without it, you'd get a compiler error (unless you rename "enum" to something else)
– Reed Copsey
Jul 9 '10 at 20:35
1
still Enum is a type declared in a different namespace even. The Enum type is yet declared outside an interface.
– Lucas Locatelli
Feb 23 '15 at 19:52
add a comment |
3
what's the @ for? I tried to look it up in MSDN...sorry if this seems trivial. I'm new to creating interfaces.
– PositiveGuy
Jul 9 '10 at 20:34
8
anticipating your question:@enumis the name of the variable. There is nothing special about that syntax
– BlueRaja - Danny Pflughoeft
Jul 9 '10 at 20:34
22
@coffeeaddict: It allows you to use a reserved word for a parameter name. Without it, you'd get a compiler error (unless you rename "enum" to something else)
– Reed Copsey
Jul 9 '10 at 20:35
1
still Enum is a type declared in a different namespace even. The Enum type is yet declared outside an interface.
– Lucas Locatelli
Feb 23 '15 at 19:52
3
3
what's the @ for? I tried to look it up in MSDN...sorry if this seems trivial. I'm new to creating interfaces.
– PositiveGuy
Jul 9 '10 at 20:34
what's the @ for? I tried to look it up in MSDN...sorry if this seems trivial. I'm new to creating interfaces.
– PositiveGuy
Jul 9 '10 at 20:34
8
8
anticipating your question:
@enum is the name of the variable. There is nothing special about that syntax– BlueRaja - Danny Pflughoeft
Jul 9 '10 at 20:34
anticipating your question:
@enum is the name of the variable. There is nothing special about that syntax– BlueRaja - Danny Pflughoeft
Jul 9 '10 at 20:34
22
22
@coffeeaddict: It allows you to use a reserved word for a parameter name. Without it, you'd get a compiler error (unless you rename "enum" to something else)
– Reed Copsey
Jul 9 '10 at 20:35
@coffeeaddict: It allows you to use a reserved word for a parameter name. Without it, you'd get a compiler error (unless you rename "enum" to something else)
– Reed Copsey
Jul 9 '10 at 20:35
1
1
still Enum is a type declared in a different namespace even. The Enum type is yet declared outside an interface.
– Lucas Locatelli
Feb 23 '15 at 19:52
still Enum is a type declared in a different namespace even. The Enum type is yet declared outside an interface.
– Lucas Locatelli
Feb 23 '15 at 19:52
add a comment |
public enum MyEnum
{
Hurr,
Durr
}
public interface MyInterface
{
void MyMethod(MyEnum value);
}
If this isn't what you're talking about doing, leave a comment so people can understand what your issue is. Because, while the enum isn't defined within the interface, this is a completely normal and acceptable design.
4
I guess the question is if the method takes a specific enum, or ANY enum...
– Will
Jul 9 '10 at 20:40
+1 I created an iEventLogTypesclassas helper for my IEventLoginterface. Right now it only contains anenum, but I can add any "types" that I need within my interface. I like sticking the enum in a "similar named" class so that the enum is more tightly bound to the specific interface, even if it's in "name only".
– franji1
Oct 16 '15 at 15:12
add a comment |
public enum MyEnum
{
Hurr,
Durr
}
public interface MyInterface
{
void MyMethod(MyEnum value);
}
If this isn't what you're talking about doing, leave a comment so people can understand what your issue is. Because, while the enum isn't defined within the interface, this is a completely normal and acceptable design.
4
I guess the question is if the method takes a specific enum, or ANY enum...
– Will
Jul 9 '10 at 20:40
+1 I created an iEventLogTypesclassas helper for my IEventLoginterface. Right now it only contains anenum, but I can add any "types" that I need within my interface. I like sticking the enum in a "similar named" class so that the enum is more tightly bound to the specific interface, even if it's in "name only".
– franji1
Oct 16 '15 at 15:12
add a comment |
public enum MyEnum
{
Hurr,
Durr
}
public interface MyInterface
{
void MyMethod(MyEnum value);
}
If this isn't what you're talking about doing, leave a comment so people can understand what your issue is. Because, while the enum isn't defined within the interface, this is a completely normal and acceptable design.
public enum MyEnum
{
Hurr,
Durr
}
public interface MyInterface
{
void MyMethod(MyEnum value);
}
If this isn't what you're talking about doing, leave a comment so people can understand what your issue is. Because, while the enum isn't defined within the interface, this is a completely normal and acceptable design.
answered Jul 9 '10 at 20:39
WillWill
112k46282369
112k46282369
4
I guess the question is if the method takes a specific enum, or ANY enum...
– Will
Jul 9 '10 at 20:40
+1 I created an iEventLogTypesclassas helper for my IEventLoginterface. Right now it only contains anenum, but I can add any "types" that I need within my interface. I like sticking the enum in a "similar named" class so that the enum is more tightly bound to the specific interface, even if it's in "name only".
– franji1
Oct 16 '15 at 15:12
add a comment |
4
I guess the question is if the method takes a specific enum, or ANY enum...
– Will
Jul 9 '10 at 20:40
+1 I created an iEventLogTypesclassas helper for my IEventLoginterface. Right now it only contains anenum, but I can add any "types" that I need within my interface. I like sticking the enum in a "similar named" class so that the enum is more tightly bound to the specific interface, even if it's in "name only".
– franji1
Oct 16 '15 at 15:12
4
4
I guess the question is if the method takes a specific enum, or ANY enum...
– Will
Jul 9 '10 at 20:40
I guess the question is if the method takes a specific enum, or ANY enum...
– Will
Jul 9 '10 at 20:40
+1 I created an iEventLogTypes
class as helper for my IEventLog interface. Right now it only contains an enum, but I can add any "types" that I need within my interface. I like sticking the enum in a "similar named" class so that the enum is more tightly bound to the specific interface, even if it's in "name only".– franji1
Oct 16 '15 at 15:12
+1 I created an iEventLogTypes
class as helper for my IEventLog interface. Right now it only contains an enum, but I can add any "types" that I need within my interface. I like sticking the enum in a "similar named" class so that the enum is more tightly bound to the specific interface, even if it's in "name only".– franji1
Oct 16 '15 at 15:12
add a comment |
Another solution could be to use Generic types:
public enum MyEnum
{
Foo,
Bar
}
public interface IDummy<EnumType>
{
void OneMethod(EnumType enumVar);
}
public class Dummy : IDummy<MyEnum>
{
public void OneMethod(MyEnum enumVar)
{
// Your code
}
}
Also, since C# 7.3, you can add a generic constraint to accept only Enum types:
public interface IDummy<EnumType> : where EnumType : Enum
{
void OneMethod(EnumType enumVar);
}
add a comment |
Another solution could be to use Generic types:
public enum MyEnum
{
Foo,
Bar
}
public interface IDummy<EnumType>
{
void OneMethod(EnumType enumVar);
}
public class Dummy : IDummy<MyEnum>
{
public void OneMethod(MyEnum enumVar)
{
// Your code
}
}
Also, since C# 7.3, you can add a generic constraint to accept only Enum types:
public interface IDummy<EnumType> : where EnumType : Enum
{
void OneMethod(EnumType enumVar);
}
add a comment |
Another solution could be to use Generic types:
public enum MyEnum
{
Foo,
Bar
}
public interface IDummy<EnumType>
{
void OneMethod(EnumType enumVar);
}
public class Dummy : IDummy<MyEnum>
{
public void OneMethod(MyEnum enumVar)
{
// Your code
}
}
Also, since C# 7.3, you can add a generic constraint to accept only Enum types:
public interface IDummy<EnumType> : where EnumType : Enum
{
void OneMethod(EnumType enumVar);
}
Another solution could be to use Generic types:
public enum MyEnum
{
Foo,
Bar
}
public interface IDummy<EnumType>
{
void OneMethod(EnumType enumVar);
}
public class Dummy : IDummy<MyEnum>
{
public void OneMethod(MyEnum enumVar)
{
// Your code
}
}
Also, since C# 7.3, you can add a generic constraint to accept only Enum types:
public interface IDummy<EnumType> : where EnumType : Enum
{
void OneMethod(EnumType enumVar);
}
edited Nov 13 '18 at 10:33
brasofilo
21.6k1068140
21.6k1068140
answered Nov 13 '18 at 9:54
Samuel LIOULTSamuel LIOULT
224214
224214
add a comment |
add a comment |
If you're talking about generic interfaces and the fact that C# doesn't let you constrain generic types to be enums, the answers to this question include two different work-arounds.
It does constrain, you just can't define the type inside the interface. The same way you do when you create an interface, add a namespace that contain a specific type then add that in the parameters or return types.
– Lucas Locatelli
Feb 23 '15 at 19:49
add a comment |
If you're talking about generic interfaces and the fact that C# doesn't let you constrain generic types to be enums, the answers to this question include two different work-arounds.
It does constrain, you just can't define the type inside the interface. The same way you do when you create an interface, add a namespace that contain a specific type then add that in the parameters or return types.
– Lucas Locatelli
Feb 23 '15 at 19:49
add a comment |
If you're talking about generic interfaces and the fact that C# doesn't let you constrain generic types to be enums, the answers to this question include two different work-arounds.
If you're talking about generic interfaces and the fact that C# doesn't let you constrain generic types to be enums, the answers to this question include two different work-arounds.
edited May 23 '17 at 10:30
Community♦
11
11
answered Jul 9 '10 at 20:51
Joel MuellerJoel Mueller
23.9k75679
23.9k75679
It does constrain, you just can't define the type inside the interface. The same way you do when you create an interface, add a namespace that contain a specific type then add that in the parameters or return types.
– Lucas Locatelli
Feb 23 '15 at 19:49
add a comment |
It does constrain, you just can't define the type inside the interface. The same way you do when you create an interface, add a namespace that contain a specific type then add that in the parameters or return types.
– Lucas Locatelli
Feb 23 '15 at 19:49
It does constrain, you just can't define the type inside the interface. The same way you do when you create an interface, add a namespace that contain a specific type then add that in the parameters or return types.
– Lucas Locatelli
Feb 23 '15 at 19:49
It does constrain, you just can't define the type inside the interface. The same way you do when you create an interface, add a namespace that contain a specific type then add that in the parameters or return types.
– Lucas Locatelli
Feb 23 '15 at 19:49
add a comment |
Defining an enum is like defining a class or defining an interface. You could just put it in one of your class files, inside the namespace but outside the class definition, but if several classes use it, which one do you put it in, and whichever you choose you will get "Type name does not match file name" warnings. So the "right" way to do it is to put it in its own file, as you would a class or an interface:
MyEnum.cs
namespace MyNamespace
{
internal enum MyEnum { Value1, Value2, Value3, Value4, Value5 };
}
Then any interfaces or classes within the namespace can access it.
add a comment |
Defining an enum is like defining a class or defining an interface. You could just put it in one of your class files, inside the namespace but outside the class definition, but if several classes use it, which one do you put it in, and whichever you choose you will get "Type name does not match file name" warnings. So the "right" way to do it is to put it in its own file, as you would a class or an interface:
MyEnum.cs
namespace MyNamespace
{
internal enum MyEnum { Value1, Value2, Value3, Value4, Value5 };
}
Then any interfaces or classes within the namespace can access it.
add a comment |
Defining an enum is like defining a class or defining an interface. You could just put it in one of your class files, inside the namespace but outside the class definition, but if several classes use it, which one do you put it in, and whichever you choose you will get "Type name does not match file name" warnings. So the "right" way to do it is to put it in its own file, as you would a class or an interface:
MyEnum.cs
namespace MyNamespace
{
internal enum MyEnum { Value1, Value2, Value3, Value4, Value5 };
}
Then any interfaces or classes within the namespace can access it.
Defining an enum is like defining a class or defining an interface. You could just put it in one of your class files, inside the namespace but outside the class definition, but if several classes use it, which one do you put it in, and whichever you choose you will get "Type name does not match file name" warnings. So the "right" way to do it is to put it in its own file, as you would a class or an interface:
MyEnum.cs
namespace MyNamespace
{
internal enum MyEnum { Value1, Value2, Value3, Value4, Value5 };
}
Then any interfaces or classes within the namespace can access it.
answered Sep 6 '17 at 15:18
DaveDave
1,43611618
1,43611618
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3216340%2fhow-to-represent-an-enum-in-an-interface-when-you-cant%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown