CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2006
    Posts
    199

    Use enum from another class?

    How do I use enum values from one class in another?

    I have this class:
    Code:
    public class class1
        {
            [FlagsAttribute]
            public enum ContainterTypes : int
            {
                CT_OUTLOOK = 0,
                CT_XML = 1,
                CT_SDX = 2,
                CT_SAP = 4
            };
        }
    and am simply trying to set a variable inside of another class to one of these enumerated values:
    Code:
    blahblah.SetContainerType(CT_XML);
    which obvously doesn't work... I have tried
    Code:
    blahblah.SetContainerType(class1.ContainterTypes.CT_XML);
    but no luck there either.

    I have set a reference to the first class in the second.


    what am I missing here?

  2. #2
    Join Date
    Sep 2006
    Posts
    199

    RESOLVED: Re: Use enum from another class?

    Code:
    blahblah.SetContainerType((int) class1.ContainterTypes.CT_XML);
    works.

    Just so I know, can someone explain why I have to cast to int since there is a reference to the class that contains the enumeration? I would think the cast would be unnecessary.

    thanks.

  3. #3
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: Use enum from another class?

    In enumeration, the enumerators are also referenced by integer values 0 - the end enumerator, so:

    Code:
    enum MyEnum { Red, Blue, Green } Colors;
    Blue can be assigned using the integer 1 just as Red = 0, Blue = 1, Green = 2, and so on.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    Re: Use enum from another class?

    What error are you getting? I assume you just coded your method incorrectly and it's throwing an error saying you can't convert from enum to int? Your method signature should look like this:

    Code:
    void SetContainerType(class1.ContainterTypes container)
    {
        if(container == class1.ContainerTypes.CT_XML)
            ConvertToXml();
    }
    I also wouldn't recommend casting your enums to and from their values for internal code, for example this is bad:

    Code:
    // This is bad
    switch((int) myEnum)
    {
         case 0:
            break;
    
        case 1:
            break;
    
        case 2:
            break;
    }
    
    
    // This is good
    switch(myEnum)
    {
         case MyEnum.Red:
            break;
    
        case MyEnum.Blue:
            break;
    
        case MyEnum.Green:
            break;
    }
    Last edited by Mutant_Fruit; September 29th, 2007 at 12:56 PM.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  5. #5
    Join Date
    Aug 2007
    Location
    Minneapolis
    Posts
    155

    Re: Use enum from another class?

    It looks like SetContainerType is expecting an int and not ContainterTypes as its parameter, so, as Mutant_Fruit suggested just change the parameter type from int to ContainerTypes and you won't need to cast.

    Also you should consider using singular for the enum type name, e.g., ContainerType instead of ContainerTypes, http://msdn2.microsoft.com/en-us/lib...01(VS.71).aspx

  6. #6
    Join Date
    Sep 2006
    Posts
    199

    Why is this so damned obtuse?

    In C++, I have a nice little header file with 4 #defines...

    All I need to do is #include the header.. then I can use the defines all over the place.. all I want to do is use some english-looking text instead of 0, 1, 2, etc.


    But in C# I gotta reference it with World. Namespace.File.Class.Somethingelse.gibberish.yadayadayad

    why is this so much more difficult???

  7. #7
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: RESOLVED: Re: Use enum from another class?

    Quote Originally Posted by purpleflash
    ....can someone explain why I have to cast to int since there is a reference to the class that contains the enumeration? I would think the cast would be unnecessary.....
    When your SetContainerType is like
    Code:
    public void SetContainerType(int type){
    .... 
    }
    Then you need to cast. as you are awaiting an integer here
    if instead of this you have
    Code:
    public void SetContainerType(ContainerTypes type){
    ...
     
    }
    then this works without troubles. May I ask why you have it this way ?
    Code:
    public class class1
    	{
    		[FlagsAttribute]
    		public enum ContainerTypes : int
    		{
    			CT_OUTLOOK = 0,
    			CT_XML = 1,
    			CT_SDX = 2,
    			CT_SAP = 4
    		};
    	}
    Instead of this
    Code:
    public enum ContainerTypes : int
    		{
    			CT_OUTLOOK = 0,
    			CT_XML = 1,
    			CT_SDX = 2,
    			CT_SAP = 4
    		};
     
    public class class1 {
    ...whatever needs using your enum here
    }
    as the enum is public its quite useless to put it into the class, isn't it ? And use singular names for enum as you in every case have one of them.
    Last edited by JonnyPoet; September 30th, 2007 at 05:18 AM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  8. #8
    Join Date
    Sep 2006
    Posts
    199

    Re: Use enum from another class?

    Ok, that makes sense.. I'll simplify it as you have suggested.

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Why is this so damned obtuse?

    Quote Originally Posted by purpleflash
    In C++, I have a nice little header file with 4 #defines...

    All I need to do is #include the header.. then I can use the defines all over the place.. all I want to do is use some english-looking text instead of 0, 1, 2, etc.


    But in C# I gotta reference it with World. Namespace.File.Class.Somethingelse.gibberish.yadayadayad

    why is this so much more difficult???
    I know you have this solved already using JonnyPoet's suggestion of the public enum, but I thought I mention a couple of things.

    In C++, your 4 #defined wasn't type safe. You had to defined the #define values and then call them in a function with

    Code:
    void SomeFn( int pseudoEnum );
    As such you had to explicitly write the range validation. In C# you get that for free (because you can't ever use a valid that isn't in the enum).

    With regard to Namespace.File.Class.Somethingelse.gibberish.yadayadayad

    You don't need to always specify the fully qualified name of the object (and I'm sure you've already figured it out). You have 3 options:

    1) Fully qualified name
    2) If the enum (const, class, etc) is in the same namespace as where you are calling it from, just call the enum (const, class, etc) directly (no ns needed)
    3) Use the 'using' statement to include the namespace in the file (if the enum or other item is in a different namespace).

  10. #10
    Join Date
    Oct 2007
    Posts
    17

    Re: Use enum from another class?

    Create an empty code file and give it a namespace and create a public enum without a class.

    Then you can utilize the namespace reference and call the public enum from any class that your namespace is referenced.

    Enums do not need to be in class files.

    In fact I use public enums all the in my business entities layer.

  11. #11
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Use enum from another class?

    Quote Originally Posted by ntfishersdk
    Create an empty code file and give it a namespace and create a public enum without a class.

    Then you can utilize the namespace reference and call the public enum from any class that your namespace is referenced.

    Enums do not need to be in class files.

    In fact I use public enums all the in my business entities layer.
    Yep, thats what I basically mentioned in post #7 to get the enums out of his classes on top of the (any ) namespace.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured