This book is based on C#, but it's always helpful to think outside of the box. Some changes have also been made to the favorite language of many developers. The first issue involved providing missing features to make VB .NET finally measure up to C#. For example, a configuration file, vbc.rsp, has been established, and it allows you to define standard parameters parallel to the C# compiler (csc.rsp). More new features are as follows:
XML documentation (Yeah, that rocks!)
Operator overloading (Ever used it?)
Partial classes
New data types
New keywords
Until now, direct source code documentation was reserved for C# developers. With the assistance of tools like VB.DOC, it was possible to integrate comments in Visual Basic .NET, too; however, this was sort of challenging, especially because no compiler or IntelliSense support was available. This will change with the eighth version of Visual Basic. From now on, code comment functionality is supported directly. This results in a simplified way to write comments and also ensures that references within the documentation are being validated.
The comments have to start with the familiar comment character (') followed by an @ symbol to make sure that the compiler will recognize them (see Listing 1-17 and Figure 1-12). After that, you can use the same XML tags as in C#. The compiler provides the new /doc option and generates the XML data, which is necessary for tools like NDoc.
Imports Microsoft.VisualBasic '@ <summary>That's my little foo class</summary> Public Class Foo '@ <summary>These methods return the text passed as parameter</summary> '@ <param name="Text">Text to be returned</param> '@ <returns>Returns the string passed as <paramref name="Text"/> parameter</returns> Public Function ReturnSomeString(ByVal Text As String) As String Return Text End Function End Class
At present, additional changes to the syntax are being considered. Possibly three apostrophe signs will be used in the final version of VB .NET 2.0 to mark comments. This would be similar to the approach of C#.
The new version of Visual Basic .NET supports generic data types in the same way as C#, but—surprise!—with a slightly different syntax. In VB .NET, this involves the new Of keyword, which has to be specified at the definition of generic classes. You also need the keyword whenever you use the generic type. Constraints are defined through the As keyword, which you already know from another context. Listing 1-18 demonstrates the realization of the Listings 1-1 and 1-2 shown earlier in the world of VB .NET.
Imports System Public Class MyList(Of ItemType As IComparable) Public Sub Add(ByVal Obj As ItemType) End Sub Default Public ReadOnly Property Items(ByVal Index As Integer) As ItemType Get Return Nothing End Get End Property End Class Public Class Foo Implements IComparable Public Function CompareTo(ByVal obj As Object) As Integer _ Implements System.IComparable.CompareTo Return 0 End Function End Class Public Class MainClass Public Shared Sub Main() Dim List As MyList(Of Foo) = New MyList(Of Foo) List.Add(New Foo) Dim AnotherFoo As Foo = List(0) Console.Read() End Sub End Class
Like C#, VB .NET now supports operator overloading. Although this feature is rarely used with custom classes in the real world, it's nevertheless fun to work with it. A small example appears in Listing 1-19. Several overloaded operators are defined to compare, add, and subtract two instances of points in the structure.
Imports System Public Structure Point Public X As Integer Public Y As Integer Public Shared Operator +(ByVal p1 As Point, ByVal p2 As Point) As Point Dim p As Point = New Point p.X = p1.X + p2.X p.Y = p1.Y + p2.Y Return p End Operator Public Shared Operator -(ByVal p1 As Point, ByVal p2 As Point) As Point Dim p As Point = New Point p.X = p1.X - p2.X p.Y = p1.Y - p2.Y Return p End Operator Public Shared Operator =(ByVal p1 As Point, ByVal p2 As Point) As Boolean Return ((p1.X = p2.X) AndAlso (p1.Y = p2.Y)) End Operator Public Shared Operator <>(ByVal p1 As Point, ByVal p2 As Point) As Boolean Return ((p1.X <> p2.X) OrElse (p1.Y <> p2.Y)) End Operator End Structure
Any operators are implemented as methods and are static by definition. Furthermore, they have to exist in pairs. Possible pairs are
= and <>
< and >
>= and <=
IsTrue and IsFalse
The operators And, Or, Not, and Xor are supported as well. Also, type conversions can be executed by such custom operators. Two new keywords are available to separate implicit (and therefore free of loss) conversion from explicit conversion: Widening and Narrowing. In Listing 1-20, you find two structures, Point2D and Point3D. Both classes allow conversion among each other, implicitly in one direction and explicitly in the other. Custom type conversions are implemented using the CType operator.
Imports System Public Structure Point2D Public X As Integer Public Y As Integer End Structure Public Structure Point3D Public X As Integer Public Y As Integer Public Z As Integer Public Shared Widening Operator CType(ByVal p1 As Point2D) As Point3D Dim p2 As New Point3D p2.X = p1.X p2.Y = p1.Y Return p2 End Operator Public Shared Narrowing Operator CType(ByVal p1 As Point3D) As Point2D Dim p2 As New Point2D p2.X = p1.X p2.Y = p1.Y Return p2 End Operator End Structure
Partial classes are available not only in C#, but also in the new VB .NET version. The required new keyword is Expands (see Listing 1-21). Unlike in C#, a main class exists in VB .NET, which has to be defined as usual. Only the additional classes have to be marked with the keyword, and they don't need any modifiers. The separation in several files is supported for classes and structures, too.
The new version of VB .NET comes with an extended pool of included data types that are now identical to the ones in C#. Please note that the following new data types aren't CLS compliant, so other languages may not be able to access them:
SByte
UInteger
ULong
UShort
There are more new keywords besides the ones already introduced. With one exception, they are all familiar to users of C#.
Continue allows you to close the current loop and jump into the next circle. The keyword can be used with For, Do, and While loops. In nested loops of different types, the desired loop could be specified by a suffix—for example, Continue For.
The very helpful using keyword (you may already know it from C#) is now available in VB .NET, too. Here, of course, it generally starts with a capital U. Using offers the definition of a dispose block. The variable defined in the header will be always disposed on leaving the scope, even in case of an unhandled exception. A frequent task that is performed is database connection, which should be returned back to the pool in any case. Another possible scenario is the use of the Font class as shown in the following listing. To enable a class to be disposed in this way, it has to implement the IDisposable interface.
Public Sub setbigbold(ByVal c As Control) Using nf As New System.Drawing.Font("Arial", 12.0F, FontStyle.Bold) c.Font = nf c.Text = "This is 12-point Arial bold" End Using End Sub
Last but not least, another new keyword is Global. This one specifies the root namespace if naming conflicts exist with the local one—for example, Global.System.Int32.