1.21. Use Anonymous Types

Problem

You need to use a simple type class that doesn't exist without actually creating it in an effort to save some development time or support LINQ, which is discussed in more detail in Chapter 6.

Solution

Instantiate a class as you would normally, using the New keyword, but do not specify a type. You must also use object initialization (see recipe 1-20) to specify at least one property.

How It Works

When you use the New keyword to instantiate an object, you typically specify the name of the type you want to create. In VB 9.0, when you omit this name, the compiler automatically generates the class for you. This class inherits from Object and overloads the ToString, GetHashCode, and Equals methods. The overloaded ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.