473,569 Members | 2,880 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

enum as index for DataRow

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
}

.....

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.

Nov 16 '05 #1
4 5729
An easy way to get around this would be to define a sealed class which
exposes the field indexes as constants or static read only values (it's up
to you how you do this). This would allow you to reduce the code as you
desire, and get the speed benefits of not having to map the field name to
the column.

The reason it doesn't work is that to .NET the enumeration is a separate
type, not the actual type that the enumeration is bounded by (int, long,
etc, etc). This is why it requires the cast.

As for whether or not which is better, that's for you to decide.
Personally, I like the purity of the approach they took, especially with the
simple workaround.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
<ma*********@AC M.ORG> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
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
}

....

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.

Nov 16 '05 #2
Marc,

Know that the method you now want to use is not the fastest to index a
datarow item.

That is with the overloaded version of the datacolumn
http://msdn.microsoft.com/library/de...itemtopic2.asp

In performance is using the overloaded methods the follows from best to
worse
datacolumn
direct index number
string

With that you have to think in nanoseconds difference in the use of it.

I hope this helps

Cor
Nov 16 '05 #3
Hi,

You either define constants:
public constant int Table1.Field1 = 0;

or you use the cast

Personally I use the first approach especially in applications running on a
PocketPC , the improvement were noticeable.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

<ma*********@AC M.ORG> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
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
}

....

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.

Nov 16 '05 #4
Could you not use a Structure with all Shared (I think Stactic in C#)
variables instead of an Enum? I think the only trick to this is that you
need at least one non-Shared (non-Static) variable in the Structure
declaration. You could make this variable a Byte so it takes up as little
memory as possible. This would work with out casting as the Structure
variables would return the correct type.

Hope this helps

Robby
VB.Net
<ma*********@AC M.ORG> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
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
}

....

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.

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
11585
by: Gregg Teehan | last post by:
Suggestions on best practive for array declare / subscript using an enum - this is easy in Delphi / Object Pascal. I want private MyObjectClass myObjects; myObjects = new MyObjectClass ; myObjects = new MyObjectClass(); myObjects = new MyObjectClass();
1
265
by: marc.gibian | last post by:
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...
5
2862
by: Barry | last post by:
Hello, In VS2003, I tried using an enum and setting it into a field in a datarow. It seems as if the datatype of the field in the row determined what went into the field. If the datatype was string, then the name of the enum item went into the field, but if the datatype was integer, then the integer value of the enum was stored. Is...
6
4245
by: Brett Romero | last post by:
I'd like to create one enum that holds webpage name values and corresponding URL values. For example: enum webPageFileName {index = "index.aspx"} enum webPageName {index = "index"} enum webPageNameTitle
7
9820
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
8
2267
by: Joe Rattz | last post by:
Ok, I can't believe what I am seeing. I am sure I do this other places with no problems, but I sure can't here. I have some code that is indexing into the ItemArray in a DataSet's DataRow. I basically want to change the value of the data stored at a particular index in the ItemArray. Ideally, I would like for my code to look like this...
3
5225
by: shapper | last post by:
Hello, I have an enum: Public Enum Color Red Blue Green End Enum
11
6368
by: =?Utf-8?B?dG9iaXdhbl9rZW5vYmk=?= | last post by:
The following code is in a custom deserializer: object value = (int) 1; string nameToParse = Enum.GetName(field.FieldType, value); value = Enum.Parse(field.FieldType, nameToParse); Currently we follow the path below: intValue --enum name --enum value
1
7087
by: John A Grandy | last post by:
To convert DataRow column values to enum types , I am using : MyEnum myEnumValue = (MyEnum )Enum.Parse( typeof( MyEnum ), ((int)row).ToString() ); Is there an easier way ?
0
7930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7681
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.