|
|
|||
| H o m e | |||
|
|
V B . N E T S t r u c t u r e S t a t e m e n t By Michael McIntyre
Visual Basic.NET provides the Structure statement so you can define your own value types. A value type defined by a structure statement is known as a ‘user-defined value type’ (UDVT).
Value types are allocated in stack memory, as opposed to reference types which are allocated in heap memory. Stack memory is optimized for size and speed whereas heap memory is optimized for storing large objects. To learn more about memory allocation for value and reference types click here: You are an OOP Programmer!
Being stack memory based, UDVTs provide a way to optimize processing speed for some operations in your applications. As a rule of thumb UDVTs should not exceed 16kb in size, but UDVTs containing a larger amount of memory can be justified in some cases.
Note: Visual Basic 6 provided the ‘Type’ statement for defining structures. The VB6 Type statement is replaced by the structure statement in VB.NET – and the rules about how to use it have changed.
EXAMPLE CODE – A Structure Statement
Structure CustomerLink ' Declare that this structure will implement ' the IComparable interface. Implements IComparable
#Region "Events" ' Add an Event member. Public Event Notify(ByVal message As String) #End Region
#Region "Fields" ' Add CustomerLastName Field member. Private _CustomerLastName As String ' Add CustomerReference Field member. ' This will hold a reference to a Customer object on the heap. Private _CustomerReference As Customer #End Region
#Region "Properties" ' CustomerLastName property. Public Property CustomerLastName() As String Get Return _CustomerLastName End Get Set(ByVal Value As String) _CustomerLastName = Value End Set End Property
' CustomerReference property. Public Property CustomerReference() As Customer Get Return _CustomerReference End Get Set(ByVal Value As Customer) _CustomerReference = Value End Set End Property #End Region
#Region "Constructors" ' Add a parameterized Constructor member. Public Sub New(ByVal customerObject As Customer) Me._CustomerReference = customerObject Me._CustomerLastName = customerObject.LastName.Substring(0, 16) End Sub #End Region
#Region "Methods" ' Add Notify User Method member that ' can raise this structure's Notify method. Public Sub NotifyUser() RaiseEvent Notify("Notify Event from structure.") End Sub
' Add CompareTo Method member that implements ' the IComparable interface so that an array of ' CustomerLinks may be sorted by CustomerLastName. Public Function CompareTo(ByVal obj As Object) As Integer _ Implements System.IComparable.CompareTo Return Me.CustomerLastName.CompareTo(CType(obj, _ CustomerLink).CustomerLastName) End Function
#End Region
End Structure
Rules
A UDVT always implicitly inherits from the System.ValueType type. So don’t even think about adding an Inherits statement to the structure statements you author.
No type can inherit from a UDVT. If you need inheritance use a class, the ‘user-defined reference type’, instead of a UDVT.
UDVTs can include events. See the Notify event in the example code.
At a minimum a UDVT must include one or more fields. See the _CustomerName and _CustomerReference fields in the example code. There is a constraint on using fields in a UDVT:
You can not initialize the value of a field in its declaration statement. Instead you can either let the compiler initialize field(s) to their default values OR add a parameterized constructor that will initialize the field values. A parameterized constructor, the New method, is demonstrated in the example code.
UDVTs can include properties. See the CustomerName and CustomerReference properties in the example code.
UDVTs can include parameterized constructors. See the New constructor in the example code. There is a constraint on using constructors in UDVTs:
UDVTs may not have parameterless constructors.
UDVTs can include methods. See the NotifyUser Sub, and the CompareTo Function, in the example code. There are some constraints on how methods can be used in a UDVT:
UDVT methods can not override methods implicitly inherited from the System.ValueType type.
UDVTs may not include destructor methods.
Every member of a UDVT must have a Public, Friend, or Private access modifier.
Summary
UDVTs are useful when you want a single variable to hold several related pieces of information. When you declare a structure, it becomes a composite data type and you can declare variables of that type. For example, you might want to keep an employee's name, telephone extension, and salary together. You could use several variables for this information, or you could define a structure and use it for a single employee UDVT.
Learn more at the links below:
|
||
|
Copyright © 2001-2005 aZ Software Developers. All rights reserved. |
|||