PC Review


Reply
Thread Tools Rate Thread

Can the index type of an array be determined?

 
 
Håkan Johansson
Guest
Posts: n/a
 
      15th Jan 2007
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


 
Reply With Quote
 
 
 
 
Scot T Brennecke
Guest
Posts: n/a
 
      15th Jan 2007
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];

"Håkan Johansson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Adam Clauss
Guest
Posts: n/a
 
      15th Jan 2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      15th Jan 2007
Scot T Brennecke <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Isn't grid order determined by tab index Bev Kaufman Microsoft Access VBA Modules 3 19th Oct 2009 12:54 AM
INDEX function (array type) Sean Microsoft Excel Worksheet Functions 4 27th Aug 2009 10:06 PM
Sheet name determined by cell value in INDEX/MATCH Shazbot Microsoft Excel Misc 2 28th Nov 2008 08:41 AM
Why can I get the index of the item of the array when I use string*, but can not get the index of the array when I use any other type (such as Int32)? Allen Maki Microsoft VC .NET 1 20th Mar 2006 10:44 AM
Looking for formula index/match-type that returns an array =?Utf-8?B?VG9t?= Microsoft Excel Worksheet Functions 1 1st Apr 2005 10:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:44 PM.