Using enumerations?

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,081
Programming Experience
10+
When working with datatables we can indicate we want changed and added rows with:

Dim chg as DataTable = realDatatable.GetChanges(Added+Modified)


Added and Modified are integer constants in the DataRowState enum..
The code above doesnt work with Option Strict on because added+modified is an integer type result not a datarowstate type result

Currently I just cast this integer result back into a datarowstate, but i cant help feeling that its just a bit messy and awkward.. If DRS is already an integer, why does the compiler take exception to me supplying it?
 
Ahhh... Thanks to all in here for some great info. The FlagsAttribute Class documentation noted by JohnH gives a great example of what Flags is used for/how it is treated as a cosmetic convenience. The article is:
http://msdn2.microsoft.com/en-us/library/system.flagsattribute.aspx

And in summary of the article, when you declare an enum as flags and give a set of constants that are powers of 2, then later you ToString a value in the enum, if it is the combination of more than one falg value, then the tostring representation gives all the values, separated by commas

If you had a Flags enum with Black=1 and White=2, and you said:
(Black Or White).ToString()
you would get a string "Black, White"

If the enum was not a Flags enum, then you would simply get "3"
 
Back
Top