Defining a non-integral enum

S

Sadeq

Enums are useful when we want to constrain the user to choose from a
specified number of predifined valuse. But they only accept integral
types as their underlying type.

I encountered a case in which I have to constrain the use to choose
from a specified number of predifined int arrays. For example:

readonly int[] choice1 ={ 1, 2, 840, 113549, 1, 1, 2 };
readonly int[] choice2 ={ 1, 2, 840, 113549, 1, 1, 4 };
readonly int[] choice3 ={ 1, 2, 840, 113549, 1, 1, 5 };

I want to define a function which only accepts these 3 arrays as its
input. Is there a way to define a signature for that function (WITHOUT
declaring its argument as int[]) to force the user to enter the correct
value at compile time?
 
V

Vladimir Matveev

Possibly you can declare class
class ArrayEnumeration
{
public static readonly ArrayEnumeration CHOICE1 =
new ArrayEnumeration(new int[] { 1, 2, 840, 113549, 1, 1, 2 });

public static readonly ArrayEnumeration CHOICE2 =
new ArrayEnumeration(new int[] { 1, 2, 840, 113549, 1, 1, 4 });

public static readonly ArrayEnumeration CHOICE3 =
new ArrayEnumeration(new int[] { 1, 2, 840, 113549, 1, 1, 5 });

private int[] _array;
private ArrayEnumeration(int[] array)
{
_array = array;
}

public int[] Array
{
get { return _array; }
}
}

and method
void Method1(ArrayEnumeration arrayEnum)
{
//use arrayEnum.Array
}
 
G

Guest

Sadeq,
If, as you indicate here, the 3 arrays are predefined, why not define them
in the class (or even inside the method) and use a switch statement where the
user passes in an enum ArrayType.Choice1, ArrayType.Choice2, etc.
In other words, "keep it simple".
?
Peter
 
N

Nick Hounsome

This version and the original both suck because the array elements can be
changed - there is no way around this with arrays - you must use a class
that implements IList<int> or similar and has no set on the indexer
 
S

Sadeq

Peter said:
Sadeq,
If, as you indicate here, the 3 arrays are predefined, why not define them
in the class (or even inside the method) and use a switch statement where the
user passes in an enum ArrayType.Choice1, ArrayType.Choice2, etc.
In other words, "keep it simple".

Good Idea for my case. But if I want to extend my class, there's a need
to add new int[] arrays, change the enum, and change the switch
statement.

Nick said:
This version and the original both suck because the array elements can be
changed - there is no way around this with arrays - you must use a class
that implements IList<int> or similar and has no set on the indexer

That seems intereting. But unfortunately I'm a little dull ;) so it
will be great if you help me more about your idea. Thnx in advance.
 
N

Nick Hounsome

That seems intereting. But unfortunately I'm a little dull ;) so it
will be great if you help me more about your idea. Thnx in advance.

class X
{
public static readonly int[] Const1 = new int[] {1,2,3};
}

X.Const1 = new int[]{42}; // error - readonly - Good
X.Const1[2] = 42; // No error! - Very bad

class X
{
public static readonly X Const1 = new X(1,2,3);

private int[] args;

// NB private ctor
private X(params int[] args)
{
this.args = args;
}

public int this[](int index)
{
get { return args[index]; }
// NB no set
}
}

X.Const1 = new int[]{42}; // error - readonly - Good
X.Const1[2] = 42; // error - problem solved

Of course you probably want to implement IEnumerable<int>, IEquatable<X> and
maybe some other sweeteners as well.

This sort of thing exists in C++ too but it can be done with enough typedefs
and consts - In C# readonly only gives you one level of constness.

Interestingly the commonest similar error in C++ is the exact oposite of
this:

static const char* Const1 = "hello world";
Const1[2] = 'a'; // error - good
Const1 = "goodbye"; // no error - bad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top