52

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?

2
  • 2
    pass SupportedPermissions.basic in parameters while calling the method n set the variable supportedPermissions to the property. Hope it helps Commented Sep 23, 2013 at 9:14
  • Reason for downvote is naming Variable like Enum. Commented Nov 27, 2017 at 13:46

4 Answers 4

58

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);
Sign up to request clarification or add additional context in comments.

Comments

7

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;
}

Comments

6

First change the method parameter Enum supportedPermissions to SupportedPermissions supportedPermissions.

Then create your file like this

file = new File
{  
    Name = name,
    Id = id,
    Description = description,
    SupportedPermissions = supportedPermissions
};

And the call to your method should be

CreateFile(id, name, description, SupportedPermissions.basic);

Comments

0
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.