enum base class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jure Bogataj

    #1

    enum base class

    Hi all!

    I have two enumerations:

    public enum MyEnum1
    and
    public enum MyEnum2

    I want to create function that accepts parameter as member of either
    enumeration. Is this at all possible?
    Something like:
    public void MyFunc(EnumBase myEnumValue)
    {
    }

    Or is there some other way to accomplish this. Actually I have arround 10
    enums and I don't want to create separate functions since the logic is the
    same?
    Maybe I can accomplish something through System.Enum?


    Best regards,
    Jure


  • Vadym Stetsiak

    #2
    Re: enum base class

    Hello, Jure!

    If your enums do not derive from other types then int, you can cast all of
    them to int.

    public enum E1
    {
    val1,
    val2,
    }
    public static MyFunc(int enumValue)
    {
    if ( E1.val1 == (E1)enumValue )
    {
    //do something
    }
    }

    MyFunc((int)E1. val1);

    --
    With best regards, Vadym Stetsiak.
    Blog: http://vadmyst.blogspot.com

    You wrote on Mon, 3 Sep 2007 16:21:18 +0200:

    JBHi all!

    JBI have two enumerations:

    JBpublic enum MyEnum1 and public enum MyEnum2

    JBI want to create function that accepts parameter as member of either
    JBenumeration. Is this at all possible?
    JBSomething like:
    JBpublic void MyFunc(EnumBase myEnumValue)
    JB{
    JB}

    JBOr is there some other way to accomplish this. Actually I have
    JBarround 10 enums and I don't want to create separate functions
    JBsince the logic is the same?
    JBMaybe I can accomplish something through System.Enum?


    JBBest regards,
    JB Jure




    Comment

    • Doug Semler

      #3
      Re: enum base class

      "Jure Bogataj" <jure.bogataj@m ikrocop.comwrot e in message
      news:eBDU1Wj7HH A.2752@TK2MSFTN GP06.phx.gbl...
      Hi all!
      >
      I have two enumerations:
      >
      public enum MyEnum1
      and
      public enum MyEnum2
      >
      I want to create function that accepts parameter as member of either
      enumeration. Is this at all possible?
      Something like:
      public void MyFunc(EnumBase myEnumValue)
      {
      }
      >
      Or is there some other way to accomplish this. Actually I have arround 10
      enums and I don't want to create separate functions since the logic is the
      same?
      Maybe I can accomplish something through System.Enum?
      >

      You could cast all of the enums to int and declare the function with the int
      parameter.

      However, I question a design that requires 10 different enumerations which,
      when cast, provide the same values for a particular functionality. If
      you're not examining the bits but need to keep the type, you can always pass
      it to your function as an object:

      void myFunc(object o)
      {
      Console.WriteLi ne(Enum.Format( o.GetType(), o, "x"));
      }
      --
      Doug Semler, MCPD
      a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
      The answer is 42; DNRC o-
      Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
      erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?

      Comment

      • Jure Bogataj

        #4
        Re: enum base class

        Actually, members of the enumeration are declared so only one bit of 32-bit
        unsigned int is set.

        e.g.

        public enum MyEnum1
        {
        enumValue1 = 0x00000001,
        enumValue2 = 0x00000002,
        enumValue3 = 0x00000004,
        enumValue4 = 0x00000008,
        enumValue5 = 0x00000010,
        enumValue6 = 0x00000020,
        }

        Then I check all enumeration / their values the same way. I just have many
        enumerations for code clarity?

        Best regards,
        Jure

        "Doug Semler" <dougsemler@gma il.comwrote in message
        news:uXCQZUn7HH A.4584@TK2MSFTN GP03.phx.gbl...
        "Jure Bogataj" <jure.bogataj@m ikrocop.comwrot e in message
        news:eBDU1Wj7HH A.2752@TK2MSFTN GP06.phx.gbl...
        >Hi all!
        >>
        >I have two enumerations:
        >>
        >public enum MyEnum1
        >and
        >public enum MyEnum2
        >>
        >I want to create function that accepts parameter as member of either
        >enumeration. Is this at all possible?
        >Something like:
        >public void MyFunc(EnumBase myEnumValue)
        >{
        >}
        >>
        >Or is there some other way to accomplish this. Actually I have arround 10
        >enums and I don't want to create separate functions since the logic is
        >the same?
        >Maybe I can accomplish something through System.Enum?
        >>
        >
        >
        You could cast all of the enums to int and declare the function with the
        int parameter.
        >
        However, I question a design that requires 10 different enumerations
        which, when cast, provide the same values for a particular functionality.
        If you're not examining the bits but need to keep the type, you can always
        pass it to your function as an object:
        >
        void myFunc(object o)
        {
        Console.WriteLi ne(Enum.Format( o.GetType(), o, "x"));
        }
        --
        Doug Semler, MCPD
        a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
        The answer is 42; DNRC o-
        Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
        erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?

        Comment

        • Doug Semler

          #5
          Re: enum base class

          On Sep 4, 3:04 am, "Jure Bogataj" <jure.boga...@m ikrocop.comwrot e:
          Actually, members of the enumeration are declared so only one bit of 32-bit
          unsigned int is set.
          >
          e.g.
          >
          public enum MyEnum1
          {
          enumValue1 = 0x00000001,
          enumValue2 = 0x00000002,
          enumValue3 = 0x00000004,
          enumValue4 = 0x00000008,
          enumValue5 = 0x00000010,
          enumValue6 = 0x00000020,
          >
          }
          >
          Then I check all enumeration / their values the same way. I just have many
          enumerations for code clarity?
          >
          Best regards,
          Jure
          First, if you are defining enum values this way, you should attribute
          the enum with the [FlagsAttribute]. If not, there is no reason to
          define the individual bit values.

          What do you mean "check their values" the same way? Enumerations are
          meant to be logical groupings. There is no real reason to have two
          enumerations have the same semantic meaning (2 exceptions, converting
          from an internal interface to a public external interface, or maybe 2
          different versions for backward compatibility). It would be
          confusing to me as a programmer to run across ten different
          enumerations that behaved exactly the same way.


          Do you mean that you have a function that does something like "Checks
          to see if bit N is set"? (e.g.:

          bool IsBitNSet(int bitfield, int bitnum)
          {
          return (bitfield & (1 << (bitnum - 1))) != 0;
          // or
          return (bitfield & bitnum) != 0;
          }

          and then in your code you have:

          if (IsBitNSet((int )myEnumVal, 1)) // or IsBitNSet((int) myEnumVal,
          0x010111)
          {
          // Do work.
          }

          I maintain it is easier to understand the programmer's intent if you
          bypass the function call conversions and/or direct bit masking:

          MyEnum bitsToCheck = MyEnum.enumValu e1 | MyEnum.enumValu e4;
          if ((myEnumVal & bitsToCheck) != MyEnum.None) // For flags, add a
          "None = 0" value.
          {
          // Do work
          }

          Comment

          Working...