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?
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).
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 .
An enum is considered an integer type. So you can assign an integer to a variable with an enum type.
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
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With