Recently, we stepped into some code on web, as follows:
var snapped = (e.Context.History & PointHistoryBits.ObjectSnapped) > 0;
Its purpose could be finally figured that the code of line tried to check whether the ObjectSnapped flag was set in the Context.History of the PointMonitor environment, through people carefully checking the definition and all enumeration values (bit flags) in the PointHistoryBits. The code had quite some assumptions actually, which are not always true.
- The PointHistoryBits.ObjectSnapped was great than zero.
- No enum values in the PointHistoryBits type was equal to zero.
- All bit flags of the other values in the PointHistoryBits looked also great than zero.
For the particular enum type PointHistoryBits, it's verified true. However, enum values can be any integers, including not only zero but also all negative ones. Nobody should or even have the right to force enum values (even bit flags) to be always positive!
In terms of the right usage of checking whether a big flag is set using enum values, it's straightforward and demonstrated here and there. Here is a source:
Enumeration Types (C# Programming Guide)
It has a good example, which addresses the exact case:
// Test value of flags using bitwise AND.
bool test = (meetingDays & Days2.Thursday) == Days2.Thursday;
Console.WriteLine("Thursday {0} a meeting day.", test == true ? "is" : "is not");
So, the good use for the AutoCAD .NET case should look like this:
bool snapped = (e.Context.History & PointHistoryBits.ObjectSnapped) == PointHistoryBits.ObjectSnapped;
It is readable, and simple at the same time. The best part is it would never fail for the task and apply to all circumstances. Apparently, enum type of big flags were not fully understood in that post, otherwise, the following should not have appeared there either:
if (e.Context.History == PointHistoryBits.LastPoint)
return;
Since the PointHistoryBits enum has some bit flags instead of normal enum values, when the point history is the LastPoint it could be ObjectSnapped also or any other combinations. Please correct us if it is not true. So, the correct usage is as follows again:
if ((e.Context.History & PointHistoryBits.LastPoint) == PointHistoryBits.LastPoint) return;
Software unreliability is accumulated by stuffs like this. A little leak will sink a great ship.
Our software such as AutoCAD .NET Addin Wizard will not have potential bugs like this, so you can trust. The latest build of AutoCAD .NET Addin Wizard 2015 can be downloaded and purchased from the following page:
Recent Comments