Tuesday, November 30, 2004

I Am Writing Some Code That I Cross Compile Under Both The Full 11 Framework And The 11 Compact Framework It Drive

I am writing some code that I cross compile under both the full 1.1 Framework and the 1.1 Compact Framework.

It drives me nuts that objects that support IDisposable in the full framework don't always support IDisposable on the Compact Framework.  This means that if I have been a good kid and written code like this:

using(SolidBrush indicatorBrush = new SolidBrush(stationColor))
{
    using (Pen indicatorPen = new Pen(stationColor))
    {
        // Draw a path 
        g.FillPolygon(indicatorBrush, points);
        g.DrawPolygon(indicatorPen, points);
    }
}

Then my code won't compile on the compact framework because Using can't cast SolidBrush to IDisposable nor can it cast Pen! I know the compact framework is compact, etc. but come on guys this is an interface with one method, Dispose that for all I care could be a no op.  So I am left with two choices.  Wrap all the objects and add the dispose interface to my wrappers.  I must admit it doesn't sound like a lot of fun.  Or write the much uglier code that does the same thing:

// Declare the objects
SolidBrush indicatorBrush = null;
Pen indicatorPen = null;
// Wrap in a try/catch so we can dispose of them on the desktop
// This is because SolidBrush and Pen don't support IDisposable on 
// the compact framework
try
{
    indicatorBrush = new SolidBrush(stationColor));
    indicatorPen = new Pen(stationColor));
    // Draw a path 
    g.FillPolygon(indicatorBrush, points);
    g.DrawPolygon(indicatorPen, points);
}
finally
{
#if !CF
    indicatorPen.Dispose();
    indicatorBrush.Dispose();
#endif
}

Has anyone checked this out in Whidbey?  Has it changed?

Tuesday, November 30, 2004 4:13:48 PM (Pacific Standard Time, UTC-08:00)   #      Comments [0]  
 

Comments are closed.

Administration
Sign In