Until now, we discussed a lot of important concepts about .NET development with Visual Basic. Just to mention some key topics, we covered class fundamentals, objects’ lifetime, types’ organization, and exceptions’ handling. We applied all these concepts to reference types (classes). But in your developer life, you will often work with value types, both built-in and custom ones. In Chapter 4, “Data Types and Expressions,” you started with the most important built-in value types in the .NET Framework, but to complete your skills you now need to know how to implement your own value types. In this chapter you get this information. You first understand structures and how to create them. Then you learn how to extend structures with custom versions of operators. Finally, you learn about enumerations, another kind of value types in .NET Framework. This is not just a simple illustration, because you also gain information on memory allocation to get a complete overview of this development area.
Structures in .NET development are the way to create custom value types. You find a lot of similarities between classes and structures, although this section explains some important differences. You create structures using a Structure..End Structure
block. The following code provides an example of a structure representing a fictitious order received by your company:
Structure can expose several members, such as fields, properties, and methods, as it happens for classes. The first important difference is about constructors. First, to create an instance of a structure, it is not mandatory to use the New
keyword as for classes. The following code shows how you can instantiate a structure:
Dim o As Order
Dim o1 As New Order
Both syntaxes are legal. The only difference is that if you don’t use the New
keyword the variable will be marked as unused until you assign a value and the VB compiler will show a warning message about this. Then you can simply assign (or invoke) members of the structure typing the name followed by a dot:
o.OrderDate = Date.Now
o.OrderID = 1
'Other assignments..
Notice that, when you declare a structure without assigning its members, the compiler assigns the structure members a default value (which is usually zero for value types and Nothing for reference types). You can also utilize the object initializers feature discussed in Chapter 7, “Class Fundamentals,” to initialize members of a structure. The following syntax is allowed but requires the specification of the New
keyword:
The second difference is that Visual Basic automatically provides an implicit constructor with no parameters, and you are not enabled to explicitly provide a constructor that receives no parameters; if you try, the Visual Basic compiler throws an error. You are instead enabled to provide a constructor that receives arguments, as follows:
As previously stated, structures can expose methods and fields but also shared members. Consider the following new implementation of the structure:
As you can see, now there is a private shared field that provides a counter for the instances of the type. Moreover, a shared method named Count
returns the number of instances of the structure. The following code snippet demonstrates how the method works:
The preceding code returns 2, because there are two active instances of the structure. Notice that the code would not work if you initialize the structure using object initializers, because the orderCount
field is incremented in the parameterized constructor.
3.138.189.5