Constructors and Default Instantiations of Value Types

Because no structure instances can exist without data, the CLR automatically creates a corresponding data instance when a structure is defined—regardless of whether New was used to instantiate the structure. The following example illustrates this point:

Dim AValue As NumberSystems
Dim AReference As ReferenceType

AValue.Value = 10
AReference.Value = 10

These lines execute with no problems until the last line, which triggers an exception because you are trying to change the property of an object that doesn’t exist on the Managed Heap, as illustrated in Figure 11-1.

Trying to change a property of an object that was not instantiated causes an exception.

Figure 11-1. Trying to change a ...

Get Microsoft® Visual Basic® 2010 Developer’s Handbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.