Visual Basic Language Changes

ASP.NET does not support Visual Basic Scripting Edition (VBScript), but it does support Visual Basic .NET, which is very similar to VBScript. However, most existing ASP pages that contain VBScript will still have to be rewritten to some extent to run under ASP.NET.

Specific Visual Basic changes that might affect existing ASP pages containing VBScript include changes to Visual Basic syntax and changes in the threading model. The following sections discuss the major changes you will have to deal with when you convert a VBScript ASP application to an ASP.NET application using Visual Basic .NET.

Visual Basic Syntax

The following list describes specific changes in Visual Basic .NET that might require changes to existing Visual Basic or VBScript applications.

  • The data type Variant no longer exists. It has been replaced with the type Object. Object types must be explicitly cast to other primitive data types.

  • Parentheses are now required around the parameter list in all method calls, even for methods that do not take parameters. For example:

    If Flag = False Then   DisplayMessage()End If
    
  • By default, arguments are passed by value, not by reference as in previous versions of Visual Basic. If you want to pass arguments by reference, you must use the ByRef keyword in front of the argument, as in the following example:

    Call MyFunction(argbyvalue, ByRef argbyref)
    
  • Set and Let are no longer supported. Objects can be assigned by a simple assignment operation:

    Object1 = Object2
    

    To set a default property of an object, you must now explicitly reference the property. For example:

  • Object1.Name = Object2.NameMost objects no longer have assumed default properties. All non-indexed properties must be explicitly referenced. In previous versions of Visual Basic, if you wanted to access the default property of an object, specifying the name of the default property was optional. For example, if you wanted to access the Text property of a TextBox control, you could use the following code:

    Dim str As String = TextBox1
    

    Because the default property of the TextBox control was the Text property. Using Visual Basic .NET, the above code must be modified as follows:

    Dim str As String = TextBox1.Text
    

    If default properties were allowed in Visual Basic .NET, an expression such as

    Object1 = Object2
    

    would be ambiguous because it is not clear whether Object2 refers to the object in its entirety or just to the default property of the object.

    As another example, you must explicitly reference the Value property of the Field object when retrieving fields from a Recordset (RS in the following example):

    Response.Write ( Server.HtmlEncode(RS("au_fname").Value))
    Indexed default properties are supported if the class contains a definition for an indexed property. The following expression is valid because the indexer makes it clear that an indexed property, not the object itself, is being referenced:
    MyProperty = Object2(6)
    
  • The Integer data type is now 32 bits; the Long data type is 64 bits.

  • Data types should always be explicitly cast to other data types. For instance, always cast numerical values to String if a string is expected:

    Response.Write("The count is " & CStr(count))
    
  • Variables created within the same Dim statement will be of the same type. For example, in Visual Basic .NET, the Dim statement Dim i, j, k As Integer creates each of the three objects (i, j, and k) as an Integer. Previous versions of Visual Basic would create i and j as Variants and k as an Integer.

  • Class property syntax has changed and no longer includes Property Let, Property Get, and Property Set. The new property syntax is similar to that in C#.

       Public Property ThisProperty As String
          Get
             ThisProperty = InternalValue
          End Get
          Set
             InternalValue = value
          End Set
       End Property
    
  • Spaces must always be included around the & operator when concatenating strings. VBScript allowed you to write a&b&c; in Visual Basic. NET this must be written as a & b & c to avoid a syntax error.

  • All If statements must be constructed on multiple lines. With VBScript, it was possible to write a single-line If statement such as If x Then y. In Visual Basic .NET, this must be written as follows:

       If x Then
          y
       End if
    
  • Option Explicit is on by default, so all variables must be declared before they can be used.

Threading

ASP.NET uses a multithreaded apartment (MTA) threading model rather than a single-threaded apartment model. COM components used in ASP.NET applications should also use the MTA model for best performance. For information about using STA components in an MTA environment, see COM Component Compatibility.

If you are using ADO.NET from ASP.NET applications, the ADO.NET components should be configured to be free-threaded (using the Both threading model). No action is required to change the threading model; the .NET Framework and Visual Studio .NET setup programs automatically change the ADO threading model to Both when they register Aspnet_isapi.dll.

You can configure ADO.NET to use the free-threaded model by importing the following registry data. To modify the registry, copy this data to a REG file (such as Adofre.reg) and import it using the Regedit.exe program (for example, Regedit /s Adofre.reg).

The following registry information is included on Windows 2000 and Windows Server 2003 systems in the Adofre15.reg file. The batch file Makfre15.bat updates the registry with the information in Adofre15.reg. Both files are located in Program Files\Common Files\System\ado\.

REGEDIT4
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{00000507-0000-0010-8000-00AA006D2EA4}\InprocServer32]
"ThreadingModel"="Both"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{00000514-0000-0010-8000-00AA006D2EA4}\InprocServer32]
"ThreadingModel"="Both"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{0000050B-0000-0010-8000-00AA006D2EA4}\InprocServer32]
"ThreadingModel"="Both"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{00000535-0000-0010-8000-00AA006D2EA4}\InprocServer32]
"ThreadingModel"="Both"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{00000541-0000-0010-8000-00AA006D2EA4}\InprocServer32]
"ThreadingModel"="Both"

If you do not plan to use ADO.NET from ASP.NET applications, you can use the apartment threading model for better ADO.NET performance with legacy applications such as those created with Visual Basic version 6. You can configure apartment threading by importing the following registry data. To modify the registry, copy this data to a REG file (such as Adoapt.reg) and import it using the Regedit.exe program.

The following registry information is included on Windows 2000 and Windows Server 2003 systems in the Adoapt15.reg file. The Makapt15.bat batch file updates the registry with the information in Adoapt15.reg. Both files are located in \Program Files\Common Files\System\ado\.

REGEDIT4
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{00000507-0000-0010-8000-00AA006D2EA4}\InprocServer32]
"ThreadingModel"="Apartment"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{00000514-0000-0010-8000-00AA006D2EA4}\InprocServer32]
"ThreadingModel"="Apartment"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{0000050B-0000-0010-8000-00AA006D2EA4}\InprocServer32]
"ThreadingModel"="Apartment"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{00000535-0000-0010-8000-00AA006D2EA4}\InprocServer32]
"ThreadingModel"="Apartment"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{00000541-0000-0010-8000-00AA006D2EA4}\InprocServer32]
"ThreadingModel"="Apartment"

See Also

Visual Basic Programming | Visual Basic .NET Language Specification | Migrating ASP Pages to ASP.NET