Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass in an enum as a method parameter

Tags:

c#

enums

I have declared an enum:

public enum SupportedPermissions {     basic,     repository,     both } 

I also have a POCO like this:

public class File {     public string Id { get; set; }     public string Name { get; set; }     public string Description { get; set; }     public SupportedPermissions SupportedPermissions { get; set; }       } 

Now I would like to create a method that I can use to create a new File object with:

public string CreateFile(string id, string name, string description, Enum supportedPermissions) {     file = new File     {           Name = name,         Id = id,         Description = description,         SupportedPermissions = supportedPermissions.basic     };      return file.Id; } 

How would I create the parameter for the enum and how would I assign that like in my pseudo code SupportedPermissions = supportedPermissions.basic so that my File instance has a SupportedPermissions set to it?

like image 915
Mike Barnes Avatar asked Sep 23 '13 09:09

Mike Barnes


People also ask

How do you pass enum value to a function in C++?

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

How do you use enums as a function?

To define enums, the enum keyword is used. enum flag {const1, const2, ..., constN}; By default, const1 is 0, const2 is 1 and so on. You can change default values of enum elements during declaration (if necessary).

How do you pass an enum in a method parameter?

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.


2 Answers

Change the signature of the CreateFile method to expect a SupportedPermissions value instead of plain Enum.

public string CreateFile(string id, string name, string description, SupportedPermissions supportedPermissions) {     file = new File     {           Name = name,         Id = id,         Description = description,         SupportedPermissions = supportedPermissions     };      return file.Id; } 

Then when you call your method you pass the SupportedPermissions value to your method

  var basicFile = CreateFile(myId, myName, myDescription, SupportedPermissions.basic); 
like image 139
Jehof Avatar answered Sep 28 '22 17:09

Jehof


If you want to pass in the value to use, you have to use the enum type you declared and directly use the supplied value:

public string CreateFile(string id, string name, string description,               /* --> */  SupportedPermissions supportedPermissions) {     file = new File     {           Name = name,         Id = id,         Description = description,         SupportedPermissions = supportedPermissions // <---     };      return file.Id; } 

If you instead want to use a fixed value, you don't need any parameter at all. Instead, directly use the enum value. The syntax is similar to a static member of a class:

public string CreateFile(string id, string name, string description) // <--- {     file = new File     {           Name = name,         Id = id,         Description = description,         SupportedPermissions = SupportedPermissions.basic // <---     };      return file.Id; } 
like image 22
Daniel Hilgarth Avatar answered Sep 28 '22 17:09

Daniel Hilgarth