Arrays in C# - indexed by an Enum, and two-dimensional as a property....

  • Thread starter Marc Scheuner [MVP ADSI]
  • Start date
M

Marc Scheuner [MVP ADSI]

Folks,

I was trying to achieve the following thing: I have a "structure" that
basically is a two-dimensional array of numeric values. Both
dimensions are handled by enumeration types in C#:

public enum Level = { low, medium, high };
public enum Foo = { bar, subbar, superbar };

Now my idea was to create a two-dimensional array using those enum's
as the dimensions, as I'm used to do in Delphi - I tried something
like:

public decimal[,] myValues = new decimal[Level, Foo];

but that didn't seem to work at all.... hmm..... so I really need to
fall back to "int"'s for array indexing?? Gosh.....

Okay, so now I tried to create a class which contains this
two-dimensional array for storage, and I'd like to make it available
to the outside world by means of a public property - indexed by both
it's dimensions, of course - again, like I used to be able to in
Delphi.

I tried:

public class MyClass
{
private decimal[,] m_Values = new decimal[3,3];

public decimal SingleValue[int iDim1, int iDim2]
{
get { return m_Values[iDim1, iDim2]; }
set { m_Values[iDim1, iDim2] = value; }
}
}

but that again didn't work.

Am I expecting too much from C#, or am I just missing an important
point somewhere along the lines of converting from Delphi to C# ??

Thanks for any hints, tips, pointers

Marc

================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
O

Ollie

Marc,

Not usre why you want to create an array that is dependent on the number of
elements in an enumeration but couldn't you do:

public decimal[,] myValues = new
decimal[Enum.GetValues(typeof(Level)).Length,
Enum.GetValues(typeof(Foo).Length];

HTH

Ollie Riches
 
L

Liam McNamara

Marc,

As regards the indexer, you have to cast the enums to ints explicitly and
you should use a C# indexer:

public decimal this[Level l, Foo f]
{
get { return myValues[(int)l, (int)f]; }
set { myValues[(int)l, (int)f] = value; }
}

The above allows you to access the elements like you would the underlying
array:

MyClass c1 = new MyClass();
decimal val = c1[Level.Low, Foo.SuperBar];

--Liam.
 
G

Guest

Hi Marc,

In addition to Ollie and Liam, i can proposition for you:

public enum Level : int = { low=0, medium=1, high=2 };
public enum Foo : int = { bar=0, subbar=1, superbar=2 };

Now you can be sure that you can cast those enums to "int"
and you can use them as "int" indices.
Now my idea was to create a two-dimensional array using those enum's
as the dimensions, as I'm used to do in Delphi - I tried something
like:

public decimal[,] myValues = new decimal[Level, Foo];

but that didn't seem to work at all.... hmm..... so I really need to
fall back to "int"'s for array indexing?? Gosh.....

Okay, so now I tried to create a class which contains this
two-dimensional array for storage, and I'd like to make it available
to the outside world by means of a public property - indexed by both
it's dimensions, of course - again, like I used to be able to in
Delphi.

I tried:

public class MyClass
{
private decimal[,] m_Values = new decimal[3,3];

Ask yourself if you don't want to write class with indexer:

public decimal this[Level level, Foo foo] {
get {
// your code
// e.g.
// return m_Values[(int) level, (int) foo];
}
set {
// your code or remove "set" part
}
}



// this will not work in C# (replace "SigleValue" with "this")
public decimal SingleValue[int iDim1, int iDim2]
{
get { return m_Values[iDim1, iDim2]; }
set { m_Values[iDim1, iDim2] = value; }
}
}

HTH
Marcin
 
J

Jon Skeet [C# MVP]

Marcin Grz?bski said:
In addition to Ollie and Liam, i can proposition for you:

public enum Level : int = { low=0, medium=1, high=2 };
public enum Foo : int = { bar=0, subbar=1, superbar=2 };

Now you can be sure that you can cast those enums to "int"
and you can use them as "int" indices.

That code is equivalent of the code he had before. The C# spec assumes
"int" as the underlying type if it's not specified, and uses 0 as the
first value and increments the value each time if it's not specified.
 
P

Peter N Roth

the C# syntax is definitely non-Delphic.

What appears to be an oversight (i.e., bug) in the
Enum design is that the 'missing' function
Enum.Count( typeof( Level ) );
is probably private or internal.

Thanks for the exercise.
--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET
 
G

Guest

Jon said:
That code is equivalent of the code he had before. The C# spec assumes
"int" as the underlying type if it's not specified, and uses 0 as the
first value and increments the value each time if it's not specified.

Yes, I know that.
But i think that my versions are more readable (besides "=" after "int")
and they describes the enum-to-int casting better, i think.

That's "my own" style of coding, so DON'T TRY IT AT HOME ;-p

Cheers!
Marcin
 

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