1.20. Use Object Initializers

Problem

You need to initialize the properties of a class when it is first instantiated, without relying on the class constructor or default values in an effort to save some development time or support LINQ, which is discussed in more detail in Chapter 6.

Solution

Instantiate a new class instance, and initialize any writable public fields or properties using the With keyword.

How It Works

VB .NET 9.0 includes the ability to initialize the writable public fields or properties of a class when it is first instantiated. When you use object initializers, the default constructor of the class is called automatically. This means any class you want to use object initializers for must have a default constructor. Any properties ...

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.