enum as index for DataRow

  • Thread starter Thread starter marc.gibian
  • Start date Start date
M

marc.gibian

I have been trying to improve the quality of my C# and ADO.NET coding.
One of the books I've read strongly advises against using string values
to address individual values in DataRow objects. This rings true to me
after years of avoiding string lookups whenever possible. But, when I
attempted to implement this recommendation I appear to run into casting
issues. Thus:

enum myFields {
field1 = 0,
field2 = 1
}

.....

Object ob = myDataRow[myFields.field1];

fails to compile, complaining the index argument cannot be converted
from myfields to int. Casting the index to int works just fine, though
I REALLY do not want to have to cast every use of this enum.

I have been doing some searching of the web and various newsgroups on
this topic. I find a general lack of discussion of this particular
issue, even though there is consensus that there is a performance gain
to be had by avoiding string lookups when possible in an easily
maintainable manner. The enum DataRow indexer technique does appear to
work well for VB.

I am beginning to wonder if the C# enum is so isolated that it is of
very limited utility. This would not be the first language to include a
constricted enum element.
 
Back
Top