Visual Basic 2008 Recipes: A Problem-Solution Approach
by Todd Herman, Allen Jones, Matthew MacDonald, Rakesh Rajan
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access