I am trying to set a property that is an enum using an integer value, like
Graphics g = this.CreateGraphics();
int enumValue = 2; // corresponds to SmoothingMode.HighQuality
g.SmoothingMode = (SmoothingMode)2; // does not have expected result
// also tried:
SmoothingMode sm = (SmoothingMode)Enum.ToObject(typeof(SmoothingMode), enumValue); // works correctly
g.SmoothingMode = sm; // still doesn't work!
But the object’s property never gets set to the correct enum value. I’ve run this through VS2010’s debugger and the casting and/or use of Enum.ToObject works correctly, but after the assignment, g.SmoothingMode is AntiAlias instead of HighQuality, for example. In fact, no matter what number I cast, the assignment always results in either AntiAlias (int equivalent 3) or None (int equivalent 4) being assigned to the object’s property.
Is there something different about when an enum is a property of a class that affects casting & assignment, or just something weird about Graphics.SmoothingMode, or what?
Take a look at SmoothingMode Enumeration description.
Quote:
So basically there are three modes:
HighQuality,AntiAlias)Default,None,HighSpeedInvalid)There is no problem with the enumeration in your code. The following line is legit.
It just internally treats
HighQualitythe same way as it doesAntiAlias.If you do:
xwill returnSmoothingMode.AntiAliasas this basically the same.