0

this might be a stupid question, but is it possible to put array into Enum?

public enum Owners { Me, Neutral, Enemy[] }

My problem is, that I don't know the number of enemies before hand, so I would love to use it like Owners.Enemy[3]. Is it this possible to do with Enum or should I just create a separate class for owners?

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
Mykybo
  • 1,429
  • 1
  • 12
  • 24
  • It sounds like you should create a separate enum for enemy types. – Lee May 15 '14 at 10:34
  • enum members must be known at compile time. If what you are doing involves stuff unknown at compile time, you can't directly map it to an enum. – Jon May 15 '14 at 10:34
  • 3
    It's not clear what `Owners.Enemy[3]` would even mean. I think you need to reconsider your design. – Jon Skeet May 15 '14 at 10:34
  • As already answered: Impossible. It's also pointless. Why ? Enums are created so that a human programmer can read "Me", "Neutral", "Enemy" and instantly know what it stands for. Whatever you're trying to do defies the logic of enum structure. – michal.ciurus May 15 '14 at 10:42

2 Answers2

0

You may create an extension method for Owners enum like such:

static Enemy[] Enemies;
static Enemy getEnemy(this Owners owner,int index){
return Enemies[index];
}
mrida
  • 1,157
  • 1
  • 10
  • 16
  • Check the following link for extending enums: http://stackoverflow.com/questions/15388072/how-to-add-extension-methods-to-enums – mrida May 15 '14 at 10:50
0

Enum might not be the right fit for what you are trying. It is a compile time representation for different values, helping you to replace magic numbers in your code by meaningful names.

Not knowing much about your application, I reckon you are probabably better off defining an object for each individual

var me = new Thingy();
var neutral = new Thingy();
var enemy1 = new Thingy();
var enemy2 = new Thingy();

And assigning the proper object to the field as you see fit, e.g.

class Something
{
    public Thingy Owner { get; set; }
}
chiccodoro
  • 14,407
  • 19
  • 87
  • 130