Reflection provides features to obtain information about loaded assemblies and the classes (and other types) within them. One of the many things you can do with reflection is to iterate through each of the properties of a class and access its values. This example uses a customer class. In C#: public class Customer { […]
Divide a List into Equal Parts
A recent post in the forums asked how to divide a generic list of items into separate lists of equal length. For example, take a list of integers as shown below. Divide the list into separate lists of equal size (plus one list for the remaining elements). In C#: List<int> dd = new List<int>() { […]
Setting WinForms Colors to a Named, RGB, or Hex Value
Sometimes you want to add a little color to your WinForms controls. WinForms comes with a large set of named colors that you can use. NOTE: Be sure to import a reference to System.Drawing. For example In C#: textBox1.BackColor = Color.Linen; In VB: textBox1.BackColor = Color.Linen But what if you want to display a color […]
Checking for Empty Strings
The "old school" way to check for empty strings is to use the Trim function to remove any empty spaces and then either check for a length of 0 or a string value of "". In C#: var str = ""; if (str.Trim().Length == 0) { MessageBox.Show("Please enter a value"); } In VB: Dim […]
Auto-Implemented Properties Part II
This prior post covered the basics of auto-implemented properties in C# and VB. This current post takes a closer look at some of the features of the auto-implemented properties and how they are different between VB and C#. C# auto-implemented properties have the following features: Getters and setters can have different access levels. Snippets are […]
Enum.TryParse
If you work with Enum values, you may often need to convert between the Enum, an integer representation of the Enum (to store in a table for example), and the string representation of the Enum (to display to the user for example). .NET 4.0 makes this easier with the new TryParse method. This example uses […]
Visual Studio: Hide Selection
Sometimes you just want to focus on a specific piece of code and you want the rest of it just out of your way. With Visual Studio 2010, you can hide any piece of code in your application using the new Hide Selection feature. For example, here is a piece of code in a SaveItem […]