Object Initializers

Since Visual Basic 2008 and .NET 3.5, the Visual Basic grammar offers another feature, named object initializers, that enables inline initialization of objects’ members when creating an instance, without the need of providing specific constructors’ overloads. For a better understanding, let’s provide a practical example. We can implement the Contact class without implementing a constructor overloads that receives any arguments for initialization:

image

To instantiate the class and initialize its members, according to the classical, old-fashioned syntax, we should write the following code:

The Visual Basic 2008 and 2010 syntax enables ...

Get Visual Basic® 2010 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.