-1

I'm new with enums and read it would be good to define enums outside of classes in the namespace.

namespace MyNamespace
{
    public static enum Index
    {
        First = 0,
        Last = 1,
    }

    public class MainPage
    {
        private MyClass[] myClasses = new MyClass[2];

        public MainPage()
        {
            myClasses[Index.First] = new MyClass();
            myClasses[Index.Last] = new MyClass();
        }
    }
    public class MyClass{}
}

What I'm doing wrong?

Edit:

Code would work by this way:

public MainPage()
{
    myClasses[(int)Index.First] = new MyClass();
    myClasses[(int)Index.Last] = new MyClass();
}

To better understand what I want, here's my "Enumeration" based on my new knowledge:

namespace OnScreenKeyboard.Enumerations
{
    namespace Key
    {
        public static class Keys
        {
            public const int Left      = 0;
            public const int Right     = 1;
            public const int Shift     = 20;
            public const int Backspace = 29;
            public const int Numbers   = 30;
            public const int Country   = 31;
            public const int Space     = 35;
            public const int Enter     = 39;
        }
    }
}

Where I can do this:

using OnScreenKeyboard.Enumerations.Key;

namespace OnScreenKeyboard
{
    public MainPage()
    {
        InitializeComponent();

        if (keyNumber == keys[Keys.Shift])
        {
            // Switch to Upper-Case Letters
        }        
    }
}

I really would like to use enumerations, but if I must always write this ugly (int) in the Code, I prefer better constants.

Druu
  • 19
  • 4

1 Answers1

0

enum is never static it just exists (with an optional access modifier).

Remove it and the rest of your code looks fine (if odd by indexing via enum, but it will work).

From the C# 5 spec:

An enum type is a distinct value type with a set of named constants. The following example declares and uses an enum type named Color with three constant values, Red, Green, and Blue.

using System;
enum Color
{
    Red,
   Green,
   Blue
}
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • I found a nice idea in the web with ExtensionMethods. With this the Indexer can be called like "Index.First.toInt()" But I think I will stay with const int, because object[First] is shorter. Anyway, thx for help :) – Druu Oct 02 '14 at 01:54
  • @Druu In general, I would not index into standard arrays using enums. The only time you don't index into an array using a number is when it is a dictionary. As an aside, I noticed you haven't accepted answers to any of your questions yet. In case you didn't know the feature exists, you can click the green check mark next to an answer to indicate that it helped you the most. It will also give you and the answerer some reputation. There is of course no obligation to do so. Anyways, glad you got it working! – BradleyDotNET Oct 02 '14 at 04:11
  • Bradley, I'm so sorry, and you are right, not good to ignore all that stuff you said. I'm so busy ... But I will try to do it better and view the Messages soon. Anyway I hope my editing will help others. – Druu Oct 02 '14 at 14:41
  • You could have just made your enum inherit from int, and it should have fixed that portion of the problem :) If you feel you have an answer to your own question, feel free to self-answer. Its preferred to putting an answer in the question itself. – BradleyDotNET Oct 02 '14 at 16:45