Simplify your business
Friday, 29 August 2008 9:18 am

VB grows up

Friday, 28 October 2005  

For experienced VB.NET developers this isn’t really anything new but having spent most of my time in the last five years writing web applications in PHP, I have to admit that I’m a bit of a late starter in this area. Having just recently worked on a new project using the .NET framework, I can tell you that as a former Visual Basic developer, I’m really impressed.

Everything has changed for the better. Visual Basic can finally claim to be an object-oriented language and a worthy competitor to C++. Yes, there’s a fair bit of new stuff to learn but it’s worth it. VB.NET benefits largely from a complete redesign. And not just in the way applications are built using the new IDE. The language is much improved too.

Structured error handling

Finally, no more stupid and clumsy On Error Goto Hell. VBN has finally implemented the much more elegant Try ... Catch ... Finally structure.

In VB6, you did this:

Sub Explode()
   Dim X As Integer, Y As Integer
   On Error Goto Hell
   X = X \ Y
Quit:
   Exit Sub
Hell:
   MsgBox Err.Description
End Sub

In VBN, you do this instead:

Sub Explode()
    Dim X, Y As Integer
    Try
      X \= Y
    Catch Ex As Exception
      MsgBox(Ex.Message)
    End Try
End Sub

Smarter boolean operations

In VB6, if you had a two part boolean And test, and the first part failed, VB6 examined the second part anyway. VBN is smarter than this: it only bothers to examine the second part, if the first part fails:

Dim Result as Boolean, X As Integer, Y As Integer
X = 1
Y = 0
If (X > 0) And ((10 / Y) > 1) Then
   Result = DoSomething()
End If

In VB6, the second part is evaluated regardless, so for the sample above, you would cop a “divide by zero” error. Not so in VBN.

Declaration changes

Finally, you can initialise your variables when you declare them.

In VB6 you had to declare and initialise you variables on separate lines:

Dim X As Integer
X = 1

In VBN, you do it all on the one line:

Dim X As Integer = 1

You can also now declare multiple variables and their data type on one line. For instance, VBN makes both variables integers:

Dim X, Y As Integer

In VB6, Y would be an integer, but X would be a variant. Finally, syntax that makes sense.

No more default properties

In VB6 it was perfectly legal to assign a string to a textbox default property:

Text1 = "Hello, World"

In VBN you can no longer do that. You have to assign the string to the “text” property instead:

Text1.Text = "Hello, World"

Although it’s a little more typing, it’s absolutely clear what property is been assigned.

Return statement

The Return statement can now be used to return a value from a function, instead of assigning something to the “pseudo” variable with the same name as the function:

In VB6, you would do this:

Function ShowNumber(ByVal X As Integer) As Integer
   ShowNumber = X + 1
End Function

In VBN, it’s much clearer what’s happening:

Function ShowNumber(ByVal X As Integer) As Integer
   Return X + 1
End Function

Array changes

Arrays are now much less confusing. First of all, in VBN they must always have a lower boundary of 0. No exceptions. Secondly, the number in the declaration is now the number of elements, not the upper boundary:

In VB6, if you left the default for arrays to start at 0, declaring an array actually gave you the upper boundary of the array, not the number of elements. For example, declaring an array like this:

Dim X(2) As Integer

In VB6, you would have an integer array with three elements, 0 to 2. In VBN, you would have an integer array with two elements, 0 to 1.

Subroutines require parentheses

Assume that you have this subroutine in both VB6 and VBN:

Sub ShowName(ByVal Name As String)
   MsgBox "The name is " & Name
End Sub

In VB6, you would call this subroutine in one of two ways:

ShowName "Hello"
Call ShowName("Hello")

In VBN, you would call this subroutine in one of two ways, but on both occasions you must use parentheses:

ShowName("Hello")
Call ShowName("Hello")

The call statement is still supported, but because it’s now even more redundant then before, it should be eliminated.

Extra assignment operators

VBN now supports shortcuts for performing certain assignment operations. In VB6, you would increment X by 1 like this:

X = X + 1

In VBN, you can also do it like this:

X += 1

VBN also supports -=, *=, /=, \=, and ^=. Looks a lot like C/C++, doesn’t it? Sadly, the ++ operator is not supported. Bummer.

Byval is now the default

Passing parameters by value instead of by reference is considered “safe programming” and is now the default, unless you explicitly state that the variable is to be passed by reference.

Optional arguments must have defaults

In VBN you can no longer declare an argument as optional and then check in the function or subroutine whether a value has been supplied with the IsMissing() function.

In VB6 you had to do this:

Sub SayHello(Optional ByVal X As String)
   If IsMissing(X) Then X = "Hello"
   MsgBox X
End Sub

Contrast that with the much cleaner syntax of VBN:

Sub SayHello(Optional ByVal X As String = "Hello")
   MsgBox(X)
End Sub

True has changed

For reasons only known to Microsoft, in VB6 (and prior) True was equal to –1. No more. False is still equal to 0, but True has now simply become Not False instead. In other words, any integer value which is not 0.

The variant is dead

The versatile (but costly) Variant data type is now an (even more versatile) Object data type. Keep in mind that speed is still an issue, when compared to explicit data types. Also, because an Object can be “nothing”, meaning that it doesn’t point to any memory address, you now have the IsNothing() function to test for instantiation:

Dim X
If IsNothing(X) Then MsgBox("X is nothing")

Set keyword is dead

The set keyword is no longer used to instantiate objects because all variables stem from classes anyway. So instead of this in VB6:

Dim Cust as Customer
Set Cust = New Customer

We simply need to do this in VBN:

Dim Cust as Customer = New Customer

Real constructors and destructors

The new subroutines Sub New() and Sub Destruct() replace the VB6 Class_Initialize() and Class_Terminate() methods. The two main benefits of using proper constructors and destructors is that they are only ever executed once, and therefore it’s possible to instantiate a class based on some dynamic parameter.

In VB6, you had to first instantiate the object and only then could you invoke the GetAddress() method:

Class Customer
   Sub Class_Initialize()
   End Sub
   Sub GetAddress(ByVal CustID As Integer)
   End Sub
End Class

Dim Cust as New Customer
Cust.GetAddress(654)

In VBN, the GetAddress() method can effectively be invoked when the object is instantiated:

Class Customer
   Sub New(ByVal CustID As Integer)
      MyBase.New()
      Me.GetAddress(CustID)
   End Sub
   Sub GetAddress(ByVal CustID As Integer)
   End Sub
End Class

Dim Cust as Customer = New Customer(654)

Class inheritance

In VBN, not only do your derived classes inherit properties and methods of the base class, but they can override the existing methods in the base class. Finally, true objects.

Function overloading

VBN allows you to effectively have a function that can accept multiple data types and do entirely different things, depending on the data type passed as an argument.

Overloads Function GetCustomer(ByVal CustName As String) As String
End Function

Overloads Function GetCustomer(ByVal CustID As Integer) As String
End Function

The function GetCustomer() can now effectively be passed a string or an integer, and the behaviour could be as different as you wish. The implementation could look something like this:

Dim Cust As String
Cust = GetCustomer("Bubba Zanetti")
Cust = GetCustomer(654)

Structures are now mini objects

Custom data types were known as UDT’s (user defined types) in VB6 and prior. In VBN, these have been renamed to “structures” and extended a little:

For instance, in VB6 you would declare a custom data type like this:

Private Type Customer
  Name As String
  Salary As Currency
End Type

In VBN, you can also incorporate methods as well, thus giving your custom data type a behaviour as well:

Structure Customer
   Dim Name As String
   Dim Income As Decimal
   Function GetTax() As Decimal
      Return 12.50
   End Function
End Structure

Namespaces

Namespaces are simply a hierarchical means of categorising classes. Because everything in the .NET framework is now a bunch of classes, and because there’s lots of them. namespaces are a handy way of grouping them together. As a developer, you can declare your objects explicitly like this:

Dim Button1 As System.Windows.Forms.Button
Dim Textbox1 As System.Windows.Forms.TextBox

Alternatively, you can also “import” a namespace, which enables you to declare your objects via a shortcut syntax:

Imports System.Windows.Forms

Dim Button1 As Button
Dim Textbox1 As Textbox

This is not a conclusuive list by any means but it pretty much covers the major changes. I can honestly say that I haven’t been this excited about a programming language since I first started dabbling in Pick, some 20 years ago.


Got something to say?

To protect your privacy, your email address will not be displayed.





Some basic rules for commenting:

  • Watch your language.
  • Keep comments on-topic and relevant.
  • You can use basic XHTML tags for formatting and linking but not bbcode.
  • Comments are moderated, so don't double post if your comment doesn't appear immediately.
  • Please proof-read your comments for spelling and grammar mistakes.
  • Watch your language.