For those reasons a lot of sample code for beginners is over simplified. This can mean problems as well. One problem is that if beginners see enough code without error handling they tend not to include it in their own projects.
What I like for sample code is very simple code that is easy to understand but that lends itself to easy expansion and improvement. I want it to be the start for experimentation. Recently I found such a sample on Code Project.
Create Pie Chart Using Graphics in C# .NET - hari19113It’s a simple C# project and even though it is not commented it should be easy for most beginners to understand. In fact I think it is easy to expand and the routines in it could be fairly easily moved to other projects. But it is far from perfect. There are some coding practices that are less than ideal. Specifically a) some variables that should not have as large a scope as they do and b) a bug that is not unrelated to that variable scope.
This article shows how to create a Pie chart using the Graphics class in C#.
Here is what happens. If you add a number of numbers to the sample program and press the View button you get a nice little pie chart like this:
Then you realize that you want a fourth number in the chart so you add it and press View again. You will get something like this:
Something is clearly wrong here. What could it be? I gave you a hint earlier. The example code can be obtained on Code Project or from my own website at www.acthompson.net/PieChart.zip I found two slightly different ways to fix this bug. One way involved adding a line while the other involved moving a line. I tend to prefer the second but I’ll leave it to the reader to find what works best for them. [Insert evil laugh here]
BTW I used Visual C# Express 2010 which is a free download. Students of Java should have little trouble trying this “exercise.”
No comments:
Post a Comment