Before embarking head-on into converting your code to .NET, there are a few changes in the language syntax and structure of which you must be aware. If you are migrating an ASP application, you must keep in mind that VBScript has been replaced with VB .NET. Considering that VB .NET is practically a new language when compared to Visual Basic and VBScript, the makers of .NET have done a very good job of ensuring backward compatibility with existing Visual Basic applications. The support, however, is not 100%. There are a few considerations that you must take into account:
Data type variant: The variant data type has been replaced with the object data type. The object data type must be explicitly cast to another data type before it can be used.
Method calls: All method calls, regardless of the number of parameters, must now use parentheses. This is true even when there is no parameter.
Me.Close()
Arguments: The default for passing arguments is now by value, as opposed to by reference. To pass arguments by reference, you must now use the ByRef keyword.
Object assignment: SET and LET keywords are no longer valid. You can now use the assignment operator.
AnObject = AnotherObject
Default property: To set the default property, you must explicitly reference the property. Previously, this was optional. However, the indexed default properties are still supported. For example, the Fields property, a default collection property of the RecordSet, does not have to be explicitly referenced.
'[Visual Basic] Dim StrObjectName as AnObject = AnotherObject RS("CustomerID").Value = "VINET" '[VB.NET] AnObject.Name = AnotherObject.Name Dim StrObjectName as AnObject = AnotherObject.Name 'This line is still supported RS("CustomerID").Value = "VINET" 'and is equivalent to RS.Fields("CustomerID ").Value = "VINET"
Integer and long: The integer data type is now 32 bits and the long data type is 64 bits.
Lazy evaluation: VB .NET now uses lazy evaluation for Boolean expressions. This means that as soon as the Boolean expression can be evaluated, the expression is not processed further. So, in an AND expression, as soon as a false value is found, the expression is no longer parsed any further and the whole expression is evaluated to False. In the case of an OR expression, as soon as a true value is found, the evaluation terminates and the whole expression is evaluated to True. This is done for speed, but you must remember that if you depend on side effects of certain functions that return Boolean values, you must nest the expression instead of using AND or OR expressions.
Explicit casting: If you need to convert from one data type to another, you must explicitly cast the data type. For example, if a string is expected, it casts another data type as a string.
Response.Write("Employees ID is: " & CStr(IntEmployeeID))
Dim statement: Variables within the same Dim statement will be of the same type.
[Visual Basic] 'A and B is a variant and only C is integer DIM A, B, C As integer [VB.NET] 'A, B and C are integer DIM A, B, C As integer
Class property syntax: The syntax no longer includes Property Get and Property Set. The new property syntax is similar to that in C#.
[Visual Basic] Public Property AProperty As String Get aProperty = APrivateVariable End Get Set APrivateVariable = value End Set End Property [VB.NET] Property AProperty( ) As String Get Return APrivateVariable End Get Set(ByVal Value As String) APrivateVariable = value End Set End Property
& operator: When using the & operator to concatenate strings, spaces must always be included.
[Visual Basic] 'No space required between & and variables AllString = String1&String2&String3 [VB.NET] 'Must have space between & and variables AllString = String1 & String2 & String3
If statements: All If statements must be on multiple lines and end with End If.
[VB Script] IF X Then Y [VB.NET] If X Then Y End If