Category Archives: classes

Make a simple event logger in C#

Usually the debugger lets you examine how a program works quite effectively, but sometimes it’s useful to use an event logger to record events for later study. For example, when you’re working with mouse events, stopping execution at a break … Continue reading

Posted in classes, debugging, files | Tagged , , , , , , , , , , | Leave a comment

Give a class default indexer properties in C#

An indexer property is a property that other code can use inside square brackets to access some value provided by the class, much as you can use the index of an array. Unlike an array, a class’s indexer need not … Continue reading

Posted in arrays, classes, syntax | Tagged , , , , , , , , , | Leave a comment

Use a TextFieldParser to read fixed-width data in C#

This example uses a TextFieldParser object to load fixed-width data from a file that contains names and addresses. Each field has a fixed width. Some records also have ZIP+4 format ZIP codes (for example, 08109-2120) and the program should discard … Continue reading

Posted in classes, database, files, parsing | Tagged , , , , , , , , , , , , , , , | Leave a comment

Use a TextFieldParser to read delimited data in C#

This example uses a TextFieldParser object to parse a data file that contains name and address data. The data contains fields delimited by commas and semi-colons. Some records also have ZIP+4 format ZIP codes (for example, 08109-2120) and the program … Continue reading

Posted in classes, database, files, parsing | Tagged , , , , , , , , , , , , , | 2 Comments

Draw a scrolling family tree in C#

The example Draw a family tree in C# shows how to draw a family tree. Unfortunately if the tree is too big, you can’t make the form big enough to display it all. One solution would be to use smaller … Continue reading

Posted in algorithms, classes, generic, graphics, OOP | Tagged , , , , , , , , , , , , , , , , , , , , , | 1 Comment

Add “extension properties” to classes in C#

Extension methods allow you to add new methods to existing classes even if you don’t have access to the classes’ source code. For example, the Randomize two-dimensional arrays in C# example shows how to add an extension method to two-dimensional … Continue reading

Posted in algorithms, classes, extension methods | Tagged , , , , , , , , , | 1 Comment

Draw trees vertically or horizontally in C#

The example Handle generic TreeNode mouse events in C# shows how to build a generic node class that can draw trees with nodes that contain just about anything. In that example, each each node is centered over its subtree. This … Continue reading

Posted in algorithms, classes, drawing, graphics | Tagged , , , , , , , , , , , , , , , , , , | 3 Comments

Override a parent class method in C#

One of the coolest features of object-oriented programming is virtual methods. If a parent class declares a method as virtual, a child class can provide a new implementation for the method. When the program calls the method for a child … Continue reading

Posted in classes, OOP | Tagged , , , , , , , , , , , , | Leave a comment

Make an initializing constructor for a child class in C#

An initializing constructor is a constructor that takes parameters to make it easy to initialize an object’s properties. This example shows how to reuse a class’s initializing constructor for a child class. The following Person class has an initializing constructor. … Continue reading

Posted in classes, OOP | Tagged , , , , , , , , , , , , , , | Leave a comment

Use the is operator in C#

The is operator lets you determine whether you can convert an object to a particular type. For example, suppose the Student class inherits from Person, and that student and person are objects from the obvious classes. Then, for example, the … Continue reading

Posted in classes, OOP, syntax | Tagged , , , , , , , , , | Leave a comment