Can the index type of an array be determined?

H

Håkan Johansson

Being new to C# I wonder if the index of an array can be some other ordinal
type than int? Coming from Delphi, I can determine the type of the index as
well as the type of the elements, but haven't had any success with C# so
far. The following example yields the compiler error: error CS0118:
'DemoClass.Months' is a 'type' but is used like a 'variable'.

class DemoClass
{
enum Months
{
january, february, march, april, may, june, july, august, september,
october, november, december
}

static void Main()
{
double[] arrIncome = new double[Months];
arrIncome[Months.january] = 1000.0;
//etc., etc..
}
}

Thank you!

Regards Håkan Johansson
 
S

Scot T Brennecke

You can use an enum value as the index of an array, but that's not what you're doing in the code
below. You are attempting to use the name of the type to indicate the size of the array! In the
case of months, we can be reasonably certain the count will always be 12, so you could hard-code
"new double[12]" when creating the array. Or, as a standard practice, include an extra identifier
in the enumerator list to be used as the size (since the values are 0-based):
enum Months
{
january, february, march, april, may, june, july, august, september,
october, november, december, MonthCount
}

then create the array as
double[] arrIncome = new double[MonthCount];
 
A

Adam Clauss

Problem is with this line:
double[] arrIncome = new double[Months];
As the error says, Months is a type, not a variable. What you need to do is
something more like:
double[] arrIncome = new double[Enum.GetValues(typeof(Months).Length];

Of course, we could be slightly more intelligent as we know there are
actually only 12 months... but the above is a generic form that can work
across any enumeration.

--
Adam Clauss

Håkan Johansson said:
Being new to C# I wonder if the index of an array can be some other
ordinal type than int? Coming from Delphi, I can determine the type of the
index as well as the type of the elements, but haven't had any success
with C# so far. The following example yields the compiler error: error
CS0118: 'DemoClass.Months' is a 'type' but is used like a 'variable'.

class DemoClass
{
enum Months
{
january, february, march, april, may, june, july, august, september,
october, november, december
}

static void Main()
{
double[] arrIncome = new double[Months];
arrIncome[Months.january] = 1000.0;
//etc., etc..
}
}

Thank you!

Regards Håkan Johansson
 
J

Jon Skeet [C# MVP]

Scot T Brennecke said:
You can use an enum value as the index of an array, but that's not what you're doing in the code
below.

No, you can't.
Or, as a standard practice, include an extra identifier in the
enumerator list to be used as the size (since the values are
0-based):

That feels like a bad way to me - it means that if you examine the
values of Months (which should be Month to fit into .NET naming
conventions) you'll find an extra value, and that the size of the
enumeration is 13 instead of 12.

Better (IMO) is to use something like:

static readonly int NumMonths = Enum.GetValues(typeof(Month)).Length;

(It's a shame enums aren't properly OO in .NET, but never mind.)

This gives the following code:

using System;

enum Month
{
January, February, March, April, May,
June, July, August, September, October,
November, December
}

class Test
{
static readonly int NumMonths = Enum.GetValues(typeof
(Month)).Length;

static void Main()
{
double[] income = new double[NumMonths];

// FAILS: income[Month.January] = 5.2;
income[(int)Month.January] = 5.2;
}
}

If you try to use the commented out line, you'll get the following
error:

Test.cs(18,16): error CS0266: Cannot implicitly convert type 'Month' to
'int'. An explicit conversion exists (are you missing a cast?)


Of course, if the OP is using .NET 2.0, he could use
Dictionary<Month,double> instead of an array to start with.
 

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