Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to optimize enum assignment in C#

Tags:

c#

enums

flags

I have got this enum

enum NetopScriptGeneratingCases
{
    AddLogMessages,
    AddLogErrors,
    AddLogJournal,
    AllLog = AddLogMessages | AddLogErrors | AddLogJournal,
    DoNothing
}

And there is UI with 3 checkboxes so depending which of them are checked I have to generate possible cases to do some job.

NetopScriptGeneratingCases netopScriptGeneratingCases = NetopScriptGeneratingCases.DoNothing;

if (checkBoxAddAuditLog.Checked)
{
    netopScriptGeneratingCases = NetopScriptGeneratingCases.AddLogJournal;
}
else if (checkBoxAddErrorLog.Checked)
{
    netopScriptGeneratingCases = NetopScriptGeneratingCases.AddLogErrors;
}
else if (checkBoxAddLogMessages.Checked)
{
    netopScriptGeneratingCases = NetopScriptGeneratingCases.AddLogMessages;
}
else if (checkBoxAddAuditLog.Checked || checkBoxAddErrorLog.Checked)
{
    netopScriptGeneratingCases = NetopScriptGeneratingCases.AddLogJournal | NetopScriptGeneratingCases.AddLogErrors;
}
else if (checkBoxAddAuditLog.Checked || checkBoxAddLogMessages.Checked)
{
    netopScriptGeneratingCases = NetopScriptGeneratingCases.AddLogJournal | NetopScriptGeneratingCases.AddLogMessages;
}
else if (checkBoxAddErrorLog.Checked || checkBoxAddLogMessages.Checked)
{
    netopScriptGeneratingCases = NetopScriptGeneratingCases.AddLogErrors | NetopScriptGeneratingCases.AddLogMessages;
}
else if (checkBoxAddErrorLog.Checked || checkBoxAddLogMessages.Checked || checkBoxAddAuditLog.Checked)
{
    netopScriptGeneratingCases = NetopScriptGeneratingCases.AddLogErrors | NetopScriptGeneratingCases.AddLogMessages | NetopScriptGeneratingCases.AddLogJournal;
}


var modifiedFiles = NetopScriptGenerator.GenerateNetopScript(netopScriptGeneratingCases, netopFiles);

But I am not sure that this code is a correct... Are there other ways to do it?

like image 482
Friend Avatar asked Mar 09 '16 12:03

Friend


People also ask

How do you change the size of an enum?

The size of an enum is compiler-specific and should default to the smallest integral type which is large enough to fit all of the values, but not larger than int. In this case, it seems the sizeof enum is fixed at 16-bit. For starters, that wastes a heck of a lot of memory if you use a boolean typedef (which I do).

Which is better #define or enum in C?

In C, enum is preferable only when we are working with natural group of constants. In all other cases it is #define and only #define .

Can we assign value to enum in C?

An enum is considered an integer type. So you can assign an integer to a variable with an enum type.


2 Answers

As mentioned in the comment, the compiler will by default give enums an incrementing integer value, which isn't suitable for using as bitflags. Try amending your definition as follows:

[Flags]
enum NetopScriptGeneratingCases
{
        DoNothing = 0,
        AddLogMessages = 1<<0,
        AddLogErrors = 1<<1,
        AddLogJournal = 1<<2,
        AllLog = AddLogMessages | AddLogErrors | AddLogJournal
}
like image 59
Chris Pickford Avatar answered Oct 29 '22 15:10

Chris Pickford


I would take what Chris suggest in his answer and assign your variable like so:

NetopScriptGeneratingCases netopScriptGeneratingCases = NetopScriptGeneratingCases.DoNothing;

if (checkBoxAddAuditLog.Checked)
{
    netopScriptGeneratingCases |= NetopScriptGeneratingCases.AddLogJournal;
}
if (checkBoxAddErrorLog.Checked)
{
    netopScriptGeneratingCases |= NetopScriptGeneratingCases.AddLogErrors;
}
if (checkBoxAddLogMessages.Checked)
{
    netopScriptGeneratingCases |= NetopScriptGeneratingCases.AddLogMessages;
}
like image 25
Thomas Ayoub Avatar answered Oct 29 '22 15:10

Thomas Ayoub