1
public enum ENUM_AccDebitCredit
{
    accDR = 1,
    accCR = 2
}

I declared this in a class as public ENUM_AccDebitCreditDebitCredit { get; set; }

In my code I tried by filling this enum from the datatable as follows

DebitCredit = (ENUM_TransactionType)Enum.Parse(typeof(ENUM_TransactionType), rsACC_AccountingRules.Tables[0].Rows[0]["DR"].ToString()) & (ENUM_TransactionType)Enum.Parse(typeof(ENUM_TransactionType), rsACC_AccountingRules.Tables[0].Rows[0]["CR"].ToString());

I would like to have multiple values to be stored in DebitCredit, how can I do this

My datatable is as follows

DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("DR", typeof(string));
table.Columns.Add("CR", typeof(string));
table.Rows.Add(25, "1","2"); // enum values
rhughes
  • 9,257
  • 11
  • 59
  • 87
Developer
  • 8,390
  • 41
  • 129
  • 238
  • 2
    possible duplicate of [Enum Flags Attribute](http://stackoverflow.com/questions/8447/enum-flags-attribute) – CodeCaster Mar 14 '13 at 13:23
  • Mark your enum with `[Flags]` attribute if you want it to behave like flags. However you need values like 1, 2, 4 (unless Return is really both Sales and Purchase, which doesn't seem likely) and use `|` to combine them – Zdeslav Vojkovic Mar 14 '13 at 13:23
  • If you want DebitCredit to be an array, check out http://stackoverflow.com/questions/7031299/array-property-syntax-in-c-sharp – David Mar 14 '13 at 13:25
  • I need to fill from Datatable values – Developer Mar 14 '13 at 13:27

4 Answers4

1

You could have a collection of ENUM_TransactionType or use [Flags] to represent each item as a bit flag.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

If you mean that you want an enumeration to "mix" more than a value, you need to change your code to:

[Flags]
public enum ENUM_TransactionType
{
   None = 0,
   ttSales = 1,
   ttPurchase = 2,
   ttReturnIn = 4
}

Also, you need to use the bit-wise OR | operator instead of AND &:

ENUM_TransactionType value = ENUM_TransactionType.ttSales | ENUM_TransactionType.ttPurchase;
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • I need to assign the values from datatable is it possible – Developer Mar 14 '13 at 13:36
  • @Dotnet Of course. If the data column is of the enum type with flags attribute and you did the whole `|` ORs, there's no problem. – Matías Fidemraizer Mar 14 '13 at 13:38
  • @MatíasFidemraizer i would consider making the property a nullable value instead of `None`. – Daniel A. White Mar 14 '13 at 13:41
  • @DanielA.White then you would have the possibility of unnecessary null reference exceptions, when a check for `.None` or `0` would suffice. – rhughes Mar 14 '13 at 13:42
  • @DanielA.White Null or "none" has different meanings. Null is "not assigned" and "none" is "no value". In bit-wise operations you're adding complexity with your proposal, because I couldn't do "SomeEnum.HasFlag(...)" if it comes null. _None_ is more semantic in this case. I would use a nullable in other cases. – Matías Fidemraizer Mar 14 '13 at 14:06
  • @DanielA.White In instance, _which is the default enum value for an enum without None?_ That's the best reason to use `None = 0`. Since .NET sets the first enum value as the default one, it makes the whole enum more predictable. – Matías Fidemraizer Mar 14 '13 at 14:07
0

Regarding your most recent edit, the following is how you would do it:

public enum ENUM_AccDebitCredit
{
    accDR = 1,
    accCR = 2
}

var type1 = (ENUM_AccDebitCredit)int.Parse(rsACC_AccountingRules.Tables[0].Rows[0]["DR"].ToString());

var type2 = (ENUM_AccDebitCredit)int.Parse(rsACC_AccountingRules.Tables[0].Rows[0]["CR"].ToString());

myClass.DebitCard = type1 | type2;

You can either use the [Flags] attribute, or you can try something like this:

public enum ENUM_TransactionType
{
    ttSales = 1,
    ttPurchase = 2,
    ttReturnIn = 4,
}

myClass.DebitCard = ENUM_TransactionType.ttSales | ENUM_TransactionType.ttReturnIn;

bool hasSales = myClass.DebitCard & ENUM_TransactionType.ttSales > 0;

From a DataTable you could try this:

var type1 = (ENUM_TransactionType)Enum.Parse(typeof(ENUM_TransactionType), rsACC_AccountingRules.Tables[0].Rows[0]["Drug"].ToString());

var type2 = (ENUM_TransactionType)Enum.Parse(typeof(ENUM_TransactionType), rsACC_AccountingRules.Tables[0].Rows[0]["Drug1"].ToString());

myClass.DebitCard = type1 | type2;

This is a process called bit masking. For more information on the maths behind this process, see here: Bit Masking.

rhughes
  • 9,257
  • 11
  • 59
  • 87
0
var DebitCredit = new List<ENUM_TransactionType>();
DebitCredit.Add((ENUM_TransactionType)Enum.Parse(typeof(ENUM_TransactionType), rsACC_AccountingRules.Tables[0].Rows[0]["Drug"].ToString()));
DebitCredit.Add((ENUM_TransactionType)Enum.Parse(typeof(ENUM_TransactionType), rsACC_AccountingRules.Tables[0].Rows[0]["Drug1"].ToString()));

Then you can test with:

if (DebitCredit.Contains(ENUM_TransactionType.ttSales)) etc.
johndsamuels
  • 329
  • 1
  • 10