On-the-Fly Code and Object Initializers

You might often use object initializers to create objects instances in line; generating code on-the-fly is powerful. Consider the following declaration:

Dim p As New Person With {.FirstName = "Alessandro",                           .LastName = "Del Sole"}

When you write this code, the Person class and its FirstName and LastName properties do not exist. At this point, you can open the correction options and select the Generate Class command. Visual Studio automatically associates the previous assignments with the class that is initialized in line and generates the appropriate members. Code generation for the previous code snippet produces the following result:

Get Visual Basic 2015 Unleashed 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.