Overview
Not entirely sure if the question is worded appropriately or not, but I previously asked this question which relates to this one: How do I correctly implement a Set in a class as a property?
I like to keep code as short, minimal and readible as possible, and this is where I think some code could be written better but I am running into problems.
An example first of 2 ways to read the value in a Set:
The long way:
if (Delphi1 in IDECompatibility) then
CheckListBox1.Checked[0] := True;
if (Delphi2 in IDECompatibility) then
CheckListBox1.Checked[1] := True;
if (Delphi3 in IDECompatibility) then
CheckListBox1.Checked[2] := True;
The cleaner, short and better way:
CheckListBox1.Checked[0] := (Delphi1 in IDECompatibility);
CheckListBox1.Checked[1] := (Delphi2 in IDECompatibility);
CheckListBox1.Checked[2] := (Delphi3 in IDECompatibility);
Now I want to do it the other way, to set the values.
Currently the only way I know is the long way:
if CheckListBox1.Checked[0] then
IDECompatibility := IDECompatibility + [Delphi1]
else
IDECompatibility := IDECompatibility - [Delphi1];
if CheckListBox1.Checked[1] then
IDECompatibility := IDECompatibility + [Delphi2]
else
IDECompatibility := IDECompatibility - [Delphi2];
if CheckListBox1.Checked[2] then
IDECompatibility := IDECompatibility + [Delphi3]
else
IDECompatibility := IDECompatibility - [Delphi3];
If possible I would like to do something like this:
IDECompatibility[Delphi1] := CheckListBox1.Checked[0]; // Array type required
IDECompatibility[Delphi2] := CheckListBox1.Checked[1]; // Array type required
IDECompatibility[Delphi3] := CheckListBox1.Checked[2]; // Array type required
There is the Exclude
and Include
members but I am unsure if these are going to be needed here or not.
So, as described above - Is there a easier way to define a Enum type based on a boolean value?
Thank you.
Delphi1
,Delphi2
, andDelphi3
meant to represent? Are you trying to offer compatibility customization to an end user based on the version of Delphi they are running? If yes, this sounds like a case for conditional compilation perhaps... – NegreteDelphi1
,Delphi2
Delphi3
is shown as, this was just for the example purposes. – EvoyTList<TSomeEnum>
- or better, a customTList<T>
; something that would prevent duplicates, etc. – Negretepublic
indexed property prefixed byIs
orHas
, in your case e.g.HasIDECompatibility
for acccess that you've shown,like this
. – Filippapublished
; which is not for you in this case) and theHas...
one as a helper for easier access. – FilippaIs
andHas
so I might check those out. – EvoyIsVisible
,HasChildren
or others with those prefixes. This is similar, but it's an indexed property by which you can read as well as write. It's just a helper property for working with a set field as it was array (similar to what you've shown). – FilippaDisplayName
andSelected
as properties organized in a list inside a SetWrapper class. Now you can easily populate this to a UI list control and read and set the enum value through the wrapper. – Weigh:= expr;
. – KneelandIDECompatibility
is eligible to be passed asvar
parameter, you can useInclude
/Exclude
intrinsics. Also, if you start with[]
, you can loseelse
branches and end up with correct set. As you see, there is a room for improvement without redesigning the whole thing. – Kneeland