enumerations

  • Thread starter Thread starter scroopy
  • Start date Start date
S

scroopy

Hi,

I'm an experienced C++ programmer but I'm new to C#. Why can't I use
enumerations like the following?

class CMyClass
{
private enum dimension:int{eWidth = 0, eHeight=1}

public int getWidth()
{
int[,] array = new int[5,5];

return array.GetLength(dimension.eWidth);
}
}

thanks
 
scroopy,

Enumerations are type safe in .NET, unlike in C++. This means that if
you want to use them as an integer, you need to perform an explicit cast.

Hope this helps.
 
Because it's an enum and not an int. You can always cast back to an
int, but why not just

public class MyClass
{
private const int widthDimension = 0;
private const int heightDimension = 1;

public int GetWidth
{
int[,] = new int[5,5];
return array.GetLength(widthDimension);
}
}


This does *really* annoy me though in the case of:

private enum someQueryFields
{
PrimaryKey = 0,
FirstName = 1,
Surname = 2
}

public Person GetPerson(...)
{
....
person.PersonId = GetNullSafeInt(dataReader,
(int)someQueryFields.PrimaryKey);
person.FirstName = GetNullSafeString(dataReader,
(int)someQueryFields.FirstName);
person.Surname = GetNullSafeString(dataReader,
(int)someQueryFields.Surname);
....
}

Where all those casts just... feel so wrong and I can't pass an Enum as
a parameter, I have to pass a someQueryFields. Gah.
 
Back
Top