locked
using enums as method parameters RRS feed

  • Question

  • User-1636816336 posted

    Hi All,

     I have this enum:

    public enum DirectoryAccess { None = 0, Read = 1, ReadWrite = 2 };

    When I call my function I would like to be able to do this:

    dir.Share(userID, DirectoryAccess.Read | DirectoryAccess.ReadWrite);

    Inside my method how do I find out what access the user has specified?

    None of the case conditions are met inside my switch statement: 

                    switch (access)
                    {
                        case DirectoryAccess.None:
                            perm = DirectoryAccess.None;
                            break;
                        case DirectoryAccess.Read:
                            perm = DirectoryAccess.Read;
                            break;
                        case DirectoryAccess.ReadWrite:
                            perm = DirectoryAccess.ReadWrite;
                            break;
                    }

    Another question:  How do I prevent users from ORing values for enum parameters?  I don't wish users to be able to do this: DirectoryAccess.None | DirectoryAccess.Read.

     

    Thanks in advance,

    Vlad
     

    Thursday, February 1, 2007 10:43 AM

Answers

  • User1502141887 posted

    Vlad,
    Use the directory Access Enum you currently have

    public enum DirectoryAccess { None = 0, Read = 1, ReadWrite = 2 };

    then your method Share should be declared like the following you may need to change some of it but the important part is declaring access of type DirectoryAccess

    public void Share(Int UserID, DirectoryAccess access)

    Then you should be able to use your switch statement you currently have.  Perm will also want to be declared as DirectoryAccess Perm
    As far as the OR I'm not to sure I would suggest either returning an error or just doing nothing if more then one is used but their is probably a better solution.

    AjaxButter

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 1, 2007 1:26 PM

All replies

  • User1502141887 posted

    Vlad,
    Use the directory Access Enum you currently have

    public enum DirectoryAccess { None = 0, Read = 1, ReadWrite = 2 };

    then your method Share should be declared like the following you may need to change some of it but the important part is declaring access of type DirectoryAccess

    public void Share(Int UserID, DirectoryAccess access)

    Then you should be able to use your switch statement you currently have.  Perm will also want to be declared as DirectoryAccess Perm
    As far as the OR I'm not to sure I would suggest either returning an error or just doing nothing if more then one is used but their is probably a better solution.

    AjaxButter

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 1, 2007 1:26 PM
  • User814240833 posted

    Read = 1d = 0001b

    ReadWrite = 2d = 0010b

    => Read | ReadWrite = 3d = 0011b 

    with d ... decimal, b ... binary

     

    I'm not sure what you want to do there but it is quite clear that none of the conditions meat if you pass (Read | ReadWrite) to the method. Because 3 is not 0, not 1 and not even 2. [:P]

    Thursday, February 1, 2007 2:30 PM
  • User-1636816336 posted

    AB, 

    I don't this ORing business.  I thought the whole idea behind enums was to guarantee the value was one of the items. 

     
    In the watch window DirectoryAccess.Read | DirectoryAccess.ReadWrite shows as 3. 

     
    I thought of using

    if (AccessType.ReadWrite.Equals(access))
       tempAccess = AccessType.ReadWrite;

     to get the highest level, but in some cases, enum items may be mutually exclusive.(Ex: Mon, Tue, Wed...)

     

    Vlad 

    Thursday, February 1, 2007 2:34 PM
  • User814240833 posted

    But why are you using that OR than?

     

    Maybe the enums become an integer if you use the operator | on them. Probably that operator is overloaded.

    Thank you for this info though. I didn't know of this issue either. What happens if you assign that (Read | ReadWrite)

    to such an enum?

    Thursday, February 1, 2007 2:55 PM