XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

Question about enums.

Last post 07/11/2009 23:12 by Craig Martin. 3 replies.
  • 07/11/2009 18:45

    Question about enums.

    Hi,

    I defined an enum in one of my classes, in C++ you access that by ClassName::EnumType but I dont know how to do that in C#. I want to pass a parameter to a function that is enum but if I just type it, it will not work since it's not known in the caller class.

    How should I address an Enum defined in a class, from another class?

    -Thanks
    Try not. Do, or do not. There is no try.
  • 07/11/2009 18:50 In reply to

    Re: Question about enums.

    Your enum must be public inside the class. Here's an example

    namespace Test
    {
        class TestClass
        {
            public enum Testenum
            {
                TestValue1
                TestValue2
            }
        }

    }

    To access this would be Test.TestClass.Testenum.TestValue1;

    You could move it outside of the class and it would be 
    Test.Testenum.TestValue1

    And you can add a using
    using Test;
    and shorten the line like this
    Testenum.Testvalue1;

    So to pass this to a method would be like this

    SomeMethodName(Testenum.TestValue1);


  • 07/11/2009 21:40 In reply to

    Re: Question about enums.

    For XNA I tend to simply get around by declaring ENUM types globaly for the most part.

    I know this may be frowned upon by some folk, however I find it's a lot easier.

    For example I have an enum that tells a spawner class I made what sort of enemy i'm going to spawn.

    Heres the code albeit sudo...

    public Enum EnemyType
    {
         BASIC
         MISSILE
         RANGED
    }

    public class EnemySpawner
    {
          Constructors.....

          void MakeMissile(EnemyType eType)
         {
                if (eType == EnemyType.BASIC)
                if (eType == EnemyType.MISSILE)
         }
    }

    Then because my Enum is public outside of the class if I want to send an Enum value its a simple case of writing...

    EnemySpawner.Spawn(EnemyType.BASIC);

    from anywhere in my code providing its in the same namespace.

    If you absolutely must have the Enum defined in your class then the only other way I can think of is to do it as was described, but for calling each enum by having to type out something as long as "Test.TestClass.Testenum.TestValue1;" I find it gets pretty messy.

    Good Luck Hope that helps.
  • 07/11/2009 23:12 In reply to

    Re: Question about enums.

    ExecutiveIguanaStudios:
    For XNA I tend to simply get around by declaring ENUM types globaly for the most part.

    I know this may be frowned upon by some folk, however I find it's a lot easier.
    It's not frowned on - it's the recommended method if the enum is public.
    Game hobbyist hell-bent on coding a diabolical Matrix
Page 1 of 1 (4 items) Previous Next