You should be aware of quite a few syntactical changes to the Visual Basic programming language before you start any project in this language. This section looks at most of the changes in Visual Basic .NET.
Let’s start out with the keyword Set. In short, it is gone. The standard object instantiation in Visual Basic is shown here:
Set objMyObj = objSomeOtherObj
In Visual Basic .NET, this code has been shortened to more closely match the C# programming language. The same code in Visual Basic .NET looks like this:
objMyObj = objSomeOtherObj
Properties have been greatly simplified in the world of the new Visual Basic. Previously, a series of properties looked like the code in Listing A.7.
Private gsString As String Private goObject as Object Public Property Let StringProp(ByVal psData as String) gsString = psData End Property Public Property Get StringProp() As String StringProp = gsString End Property Public Property Set ObjectProp(ByVal poObj as Object) Set goObject = poObj End Property |
In Visual Basic .NET, this is much shorter; an example is given in Listing A .8.
Private gsString as String Private goObject as Object Public Property StringProp as String Get StringProp = gsString End Get Set gsString = StringProp End Set End Property |
As you can see, there is no longer a distinction between a Set and a Let because of the change mentioned in the last section.
Calls of all types (function, method, and sub) must use parentheses around the parameters, regardless of whether you are doing something with the return value. For example, the code in Listing A .9 would work in Visual Basic or VBScript.
dtDate = Date MyFunction "Value1", 2, plVal |
However, in Visual Basic .NET, you would need to change these same calls to the code shown in Listing A.10.
dtDate = Date() MyFunction("Value1", 2, plVal) |
A major change to the ways parameters are passed has been introduced into Visual Basic .NET. Previously, all parameters were passed ByRef if no method was specified. Now, all intrinsic types are passed ByVal. So, the function in Listing A.11 would no longer work in Visual Basic .NET.
Sub MyFunction(plLng1 As Long, plLng2 As Long, plLng3 As Long) plLng3 = plLng1 + plLng2 End Sub |
In Visual Basic .NET, this subroutine would have to be rewritten as shown in Listing A.12.
Sub MyFunction(plLng1 As Long, plLng2 As Long, ByRef plLng3 as Long) plLng3 = plLng1 + plLng2 End Sub |
Unlike VBScript, Visual Basic supports a wide range of variable types. The same can be said of Visual Basic .NET. In previous server-side code you have written, all variables were declared as type Variant. With your server-side code written in Visual Basic .NET, it is highly recommended that, when you’re declaring variables, you define them appropriately so that they will not use excess memory and will be far more efficient. Listing A.13 shows a few examples of how to declare variables of specific types.
Dim psString As String Dim plLong as Long Dim bByte as Byte |
Also be aware that the Currency datatype has been removed.
Something else to note is that the sizes of certain intrinsic datatypes have changed. Refer to Table A.2 for more information.
Datatype | Size |
---|---|
Byte | 1 byte (8 bits) |
Short | 2 bytes (16 bits) |
Integer | 4 bytes (32 bits) |
Long | 8 bytes (64 bits) |
Single | 4 bytes (32 bits) |
Double | 8 bytes (64 bits) |
Decimal | 12 bytes (96 bits) |
Note here that the sizes for Integer and Long datatypes have changed. Long is now 64 bits instead of 32 bits, and Integer is now 32 bits rather than 16 bits.
The Variant datatype no longer exists in Visual Basic .NET. It has been replaced with the universal Object type. Also removed from Visual Basic .NET is the VarType function. Now, to get the type of a specific variable, you can use the following property that is a member of all the intrinsic datatypes:
SomeObj.GetType.GetTypeCode.value
A new feature of Visual Basic .NET is the capability to initialize variables and arrays at the time of declaration, as shown in Listing A.14.
Dim psString As String = "Hello!" Dim piInt as Integer = 123 Const cSTRING = "Goodbye!" Dim psArray(2) As String = ("Brian", "Jon") |
Note, however, that the capability to declare strings of a predefined length is missing in Visual Basic .NET. Therefore, the statement Dim psString As String * 5 is no longer valid.
Visual Basic .NET now supports shorthand assignment much like C, C++, and Java. The code in Listing A.15 illustrates a few examples of the shorthand notation.
plVal = 100 plVal += 10 ' plVal now equals 110 plVal −= 10 ' plVal now equals 100 plVal *= 5 ' plVal now equals 500 plVal /= 5 ' plVal now equals 100 |
Although the standard On Error GoTo XXX and On Error Resume Next exist in Visual Basic .NET, you also might want to take advantage of its built-in structured error handling, which is similar to that of languages such as C++ and Java. The code in Listing A.16 shows an example of structured error handling in Visual Basic .NET.
Try ' Some code Catch ' What to run when an error occurs Finally ' Code that always executes after try or catch End Try |
In Visual Basic, structures were defined using the Type…End Type, as shown in Listing A.17.
Type Employee EmpName As String EmpNumber As Long EmpAge As Integer End Type |
In the new Visual Basic .NET, this same structure would be declared using the Structure…End Structure keywords, as shown in Listing A.18.
Structure Employee EmpName As String EmpNumber As Long EmpAge As Integer End Structure |
The scope of variables in Visual Basic .NET is slightly different from that of Visual Basic. In Visual Basic, the code in Listing A.19 would be valid.
For plCount = 0 to 10 Dim plVal as Long plVal = plVal + plCount Next plVal2 = 2 ^ plVal |
In Visual Basic .NET, however, because the variable plVal is declared inside the For…Next loop, its scope is inside that block. Therefore, it cannot be seen outside the loop in the previous example.
In Visual Basic, the following statement would declare an object and set it to Nothing until it was used. At that point, it would reference a new instance of MyObject.
Dim poObject as New MyObject
In Visual Basic .NET, however, this statement is actually shorthand for the following:
Dim poObject As MyObject = New MyObject
In this statement, the object is created and references a new instance of the object.
Ironically, in Visual Basic .NET, IsMissing is, well, missing. This means that all optional parameters to a function must be declared with a default value, as follows:
Sub MySub(Optional psStr = "Default value!")
Certain control-of-flow tatements have been removed from Visual Basic .NET, including these:
GoSub
On…GoSub
On…GoTo (Error…GoTo is still valid)
A change also has been made to the While loop. Previously, While…Wend was valid; however, now the syntax has changed to While…End While.
To wrap this all up, here is a list of everything that has been removed from the Visual Basic .NET programming language:
As Any keyword phrase
Atn function
Calendar property
Circle statement
Currency datatype
Debug.Assert method
Debug.Print method
Deftype statements
DoEvents function
Empty keyword
Eqv operator
GoSub statement
Imp operator
Initialize event
Instancing property
IsEmpty function
IsMissing function
IsNull function
IsObject function
Let statement
Line statement
LSet statement
MsgBox function
Null keyword
On … GoSub construction
On … GoTo construction
Option Base statement
Option Private Module statement
Property Get , Property Let, and Property Set statements
PSet method
Rnd function
Round function
RSet statement
Scale method
Set statement
Sgn function
Sqr function
String function
Terminate event
Time function and statement
Type statement
Variant datatype
VarType function
Wend keyword