I've been working on a Compact Framework 1.0 project for a customer and doing some custom control work (yes CF1.0 even though CF3.5 is coming). I've done a lot of custom controls using Compact Framework 2.0 and have gotten used to the new methods/properties available so going back to 1.0 takes a little bit of getting used to.
That said, I was using a SolidBrush and in CF1.0 it does not implement IDisposable whereas in CF2.0 it does. At first I ended up with code like this.
SolidBrush brush = new SolidBrush(Color.Black);
//Drawing code here
brush.Dispose();
This will work, but when you have a lot of brushes you are creating it also starts creating a lot of code. You are probably thinking why not just use the using statment since SolidBrush has a Dispose() method like this.
using (SolidBrush brush = new SolidBrush(Color.Black))
//Drawing code here
Although in CF1.0 SolidBrush does have a Dispose() method, it does not implement IDisposable so using the using statement is out. So how do you overcome this? I created a 'wrapper' to the SolidBrush. Here is an implementation:
public class SolidBrush2 : IDisposable
{
private SolidBrush brush;
public SolidBrush2(Color color)
{
this.brush = new SolidBrush(color);
}
public SolidBrush Brush
{
get
{
return brush;
}
}
#region IDisposable Members
public void Dispose()
{
brush.Dispose();
}
#endregion
}
You'll notice a few things. First, our SolidBrush2 class implements IDisposable. Second, we have a private SolidBrush variable that holds the actual SolidBrush object used for drawing. With this class we can now use using like this:
using (SolidBrush2 brush = new SolidBrush2(Color.Black))
//Drawing code here
So we eliminated the manual calling of Dispose() by using the using statement (for another example of using statement see here). Great, but I still wasn't happy. With the SolidBrush2 implementation we would still have code like this:
using (SolidBrush2 brush = new SolidBrush2(Color.Black))
e.Graphics.DrawString("Test", this.Font, brush.Brush, 1, 1);
Notice the brush.Brush in the DrawString() method? If the customer would ever update to CF2.0 (which I'm trying to get them to do) there will be a lot of code to change. If this does happen, first SolidBrush2 would be changed to SolidBrush. Then all the brush.Brush statements would have to be changed to just brush. This means grunt work for me and wasted money for the customer (since they would pay me for the grunt work).
implicit operator to the resuce!! By adding an implicit operator to our SolidBrush2 class we can return the internal SolidBrush member variable.
public static implicit operator SolidBrush(SolidBrush2 sb2)
{
return sb2.Brush;
}
And when using our SolidBrush2 in a using statement we can get rid of the '.Brush' like so:
using (SolidBrush2 brush = new SolidBrush2(Color.Black))
e.Graphics.DrawString("Test", this.Font, brush, 1, 1);
The results, cleaner code, less grunt work for me if the customer decides to upgrade CF2.0, and saved money for the customer since all I would have to do is a Find&Replace on SolidBrush2 to SolidBrush.
Here is the final SolidBrush2 implementation:
public class SolidBrush2 : IDisposable
{
private SolidBrush brush;
public SolidBrush2(Color color)
{
this.brush = new SolidBrush(color);
}
public static implicit operator SolidBrush(SolidBrush2 sb2)
{
return sb2.Brush;
}
public SolidBrush Brush
{
get
{
return brush;
}
}
#region IDisposable Members
public void Dispose()
{
brush.Dispose();
}
#endregion
}Enjoy!