471,337 Members | 841 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Defining a non-integral enum

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?

Jan 31 '06 #1
5 1534

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
}

Jan 31 '06 #2
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Sadeq" wrote:
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?

Jan 31 '06 #3
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

"Vladimir Matveev" <de******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...

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
}

Jan 31 '06 #4
Peter Bromberg [C# MVP] wrote:
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 Hounsome wrote: 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.

Feb 1 '06 #5

"Sadeq" <MS******@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Nick Hounsome wrote:
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.


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
Feb 1 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Senthilraja | last post: by
16 posts views Thread by Raj Kotaru | last post: by
13 posts views Thread by John | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.