Monthly Archives: August 2012

Create a class outside of any namespace statement in C#

When you create a class in C#, Visual Studio automatically puts it inside a namespace statement. If your project’s name is howto_remove_namespace, then by default that namespace is howto_remove_namespace. Did you know that you can create classes outside of any … Continue reading

Posted in classes, syntax | 2 Comments

Book Review: The Manga Guide to Linear Algebra

(Occasionally I post reviews of book that I think may interest programmers, in this case admittedly those with a mathematical inclination. I’ve been fairly remiss about not posting this one sooner.) The Manga Guide to Linear Algebra   $24.95, 264 … Continue reading

Posted in books, mathematics | Leave a comment

Generate random polygons in C#

I needed a method for generating random polygons for a graphics program and the following code shows the result. // Make a random polygon inside the bounding rectangle. private static Random rand = new Random(); public static PointF[] MakeRandomPolygon( int … Continue reading

Posted in algorithms, geometry, graphics | Leave a comment

Add extension methods to the Random class to generate double values within a specified range in C#

The Random class provides methods for generating pseudo-random numbers. The Next method has three overloaded versions that produce a non-negative integer, an integer between 0 and some upper bound, and between lower and upper bounds. Strangely the Random class’s NextDouble … Continue reading

Posted in extension methods | Leave a comment

Make MidX, MidY, and Center extension methods for the Rectangle and RectangleF structs in C#

The Rectangle and RectangleF structs have several useful properties for determining their geometry such as Left, Right, Top, and Bottom, but they have no midpoint properties. The following code adds MidX and MidY methods to these structs so your code … Continue reading

Posted in extension methods, graphics | Leave a comment

Use a foreach loop to iterate over the items in an array or list in C#

You can use a foreach loop to iterate over the items in arrays, lists, collections, and other objects that have a GetEnumerator method. These loops are often more convenient than for or while loops because you don’t need to worry … Continue reading

Posted in arrays, lists, syntax | Leave a comment

Print the contents of a ListView control with data that doesn’t fit in one line in C#

The example Print the contents of a ListView control in C# explains how to print the data in a ListView control when its View property is set to Details. One difficulty when printing this kind of data is that a … Continue reading

Posted in controls, printing | Leave a comment

Print the contents of a ListView control in C#

One of the problems with printing is that it is very flexible so it’s hard to anticipate what you might want to do with it. For example, suppose a ListView control contains too many columns to fit on one page. … Continue reading

Posted in controls, printing | 22 Comments

Use string extension methods to validate Social Security numbers in C#

Recall that you must add extension methods to a static class and that the methods must be static. This example begins by defining a Matches string extension method to determine whether a string matches a regular expression. using System.Text.RegularExpressions; … … Continue reading

Posted in extension methods, extensions, regular expressions, strings | Leave a comment

Center a form on the screen in C#

You could use the size of the form and the location and size of the task bar to figure out where to position the form to center it, but there’s a much easier way to do this. Simply invoke the … Continue reading

Posted in forms | Leave a comment