Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing An Enum Type As An Argument? [duplicate]

Tags:

c#

enums

winforms

Possible Duplicate:
C# enums as function parameters?

I was wondering how I can pass an enum type as a method argument.

I'm trying to create a generic method that will take a combo box, and enum, and fill the combo box with each item of the enum.

like image 689
sooprise Avatar asked Jun 23 '11 13:06

sooprise


People also ask

Can enum have duplicate values?

Enums can not have duplicate values.

How do you pass an enum in a method argument?

Change the signature of the CreateFile method to expect a SupportedPermissions value instead of plain Enum. Show activity on this post. Show activity on this post. First change the method parameter Enum supportedPermissions to SupportedPermissions supportedPermissions .

How do I pass enum as reference?

You can use the operator & ( bitwise and ) and ( | bitwise or ) and use each bit as a bool. Remind that a enum behaves as a int. For example, if the user has pressed the keys W and S. Show activity on this post.

How do you pass an enum in a function C++?

static class Myclass { ... public: enum encoding { BINARY, ASCII, ALNUM, NUM }; Myclass(Myclass::encoding); ... } Then in the method definition: Myclass::Myclass(Myclass::encoding enc) { ... }


1 Answers

I think this is best explained by an example:

Say you have an enum:

enum MyEnum
{
    One,
    Two,
    Three
}

You can declare a method like:

    public static void MyEnumMethod(Enum e)
    {
        var enumValues = Enum.GetValues(e.GetType());

        // you can iterate over enumValues with foreach
    }

And you would call it like so:

MyEnumMethod(new MyEnum());
like image 154
cbley Avatar answered Oct 24 '22 07:10

cbley