F.1. Enhancements of Existing FunctionalitySome of the changes included in Visual Basic 2005 modify or enhance existing Visual Basic features. F.1.1. Custom Event StatementEvents in Visual Basic are linked to event handlers either through the AddHandler statement or with the Handles clause on a member declaration. The process of adding and removing handlers is fully managed by the framework, as is the process of calling handlers when events occur. Visual Basic 2005 adds a new custom events feature that allows your code to partner with the .NET Framework in the management of adding and removing handler, and in calling event handlers. The general syntax of a custom event handler is as follows: accessModifier Custom Event eventName As EventHandler AddHandler(ByVal value As EventHandler) ' ----- Special code when adding handlers. End AddHandler RemoveHandler(ByVal value As EventHandler) ' ----- Special code when removing handlers. End RemoveHandler RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs) ' ----- Special code when raising the event. End RaiseEvent End Event Your own custom code can be included in any of the three event procedures. Custom events are discussed in Chapter 8. F.1.2. Data-Type AdditionsVisual Basic 2005 includes four new data types : SByte (signed byte), UShort (unsigned short), UInteger (unsigned integer), and ULong (unsigned long). Actually, these four data types existed in the .NET Framework from the initial release of .NET and could even be used from Visual Basic code. However, Visual Basic wrappers were not included for these data types as they were for all of the other core data types. These four new data types complete Visual Basic's implementation of the Base Class Library's basic data types. These four data types are not compliant with the minimal Common Language Specification. Components and applications using that standard may not be compatible with applications that use SByte, UShort, UInteger, or ULong. Additional information about these data types is available in Chapter 4. F.1.3. Global KeywordIf your application includes a namespace named something like MyCompany.System, and you use the Imports MyCompany statement in your code file, any reference to "System" becomes ambiguous. To resolve possible conflicts such as these, Visual Basic 2005 includes a new Global keyword. When potential conflicts exist, the Global keyword can be used to provide a true top-level reference to the desired namespace-based type. To access the .NET-supplied System namespace, reference Global.System, which removes any ambiguity conflicts with MyCompany.System. F.1.4. IsNot OperatorVisual Basic has always included an Is operator, used to establish the equivalence of an object with another object or type. This operator is most often used in If statements. If (someInstance Is SomeClass) Then The operator is also used to test for an uninitialized object. If (someInstance Is Nothing) Then However, to reverse the statement to test for something that the object "is not," the Not operator had to be used separately. If Not (someInstance Is SomeClass) Then Visual Basic 2005 adds a new IsNot operator. If (someInstance IsNot SomeClass) Then The IsNot operator is functionally equivalent to the combined Is and Not operators, but it was added to increase readability of the source code. F.1.5. Lower Bound in Array DeclarationsPre-.NET versions of Visual Basic allowed both a lower bound and an upper bound in the sizing of an array dimension. Dim dataArray(0 To 5) As Integer Visual Basic .NET 2002 removed the lower bound declaration and the To keyword, since all arrays had a lower bound of zero. Dim dataArray(5) As Integer The 2005 update to Visual Basic restores the lower bound element and the To keyword; they can now be included, although they are optional. When included, the lower bound must still always be zero. F.1.6. Partial TypesIn most Visual Basic code, each source-code file includes a single class or other similar type. The language also permitted any number of classes or types to appear in a single source code file, but a class could not be split between multiple source-code files. This has changed with the 2005 release. Visual Basic now permits classes and structures to be split across multiple source-code files in the same project. This change permits Microsoft to move the Windows Forms-specific initialization code, previously contained in a #Region block, into its own source code file. To use this feature, begin a class or structure definition with the new Partial keyword. Partial Friend Class Employee ... End Class At least one of the class portions must include the Partial keyword. See the "Partial Keyword" entry in Chapter 12 for additional usage information. F.1.7. Property Accessor EnhancementsIn Visual Basic .NET 2002 and 2003, the Get and Set portions of a Property member always had the same level of access, such as Public or Private. The 2005 update to the language allows each of the two portions to have different levels of accessibility. The syntax is generally unchanged, with a primary access modifier included with the Property statement itself. But an additional access modifier can be added to the start of the Get or Set statement. Public Property Name() As String Get Return fullName End Get Friend Set(ByVal value As String) fullName = value End Set End Property The additional access modifier must always be more restrictive than the general modifier used for the Property statement itself. F.1.8. TryCast FunctionVisual Basic permits general conversions of one class or data type to another using either the CType function or the DirectCast function. These functions both generate exceptions if the conversion cannot be performed successfully. While an exception is a clear indication of a failed conversion, it is not the most elegant or straightforward, as you must set up explicit error handlers for such conditions. Therefore, Visual Basic 2005 includes a new tryCast function. It works just like a CType or DirectCast function, but it returns Nothing on failed conversions instead of generating an exception. newObject = tryCast(origObject, NewType) |