When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Wednesday, May 30, 2001

Overview of Changes from VB6 to VB.NET, Part 2

By Bipin Joshi


  • Read Part 1

  • In Part 1 we examined VB.NET's changes to data types, variable declaration, and arrays. In this part we'll continue our examination of VB.NET's syntax changes.

    - continued -

    Variable Scoping
    Consider following VB6 code:

    If x=y then
      Dim z as integer
      ' other code 
    End If
    
    z=100  'Outside of If ... Then block
    

    The above code runs perfectly with VB6 because there is no block-level variable scoping. (Block-level scoping occurs in other modern programming languages (such as C++). Variables defined within statement blocks, such as the variable defined in the If ... Then block, fall out of scope when the statement block ends. Therefore, accessing z outside of the If ... Then block where it's defined, would cause an error in modern programming languages.) VB.NET, contrary to VB6, employs block-level variable scoping.

    The Set and Let Statements
    In VB6 you must use the Set statement to assign an object instance to a variable. This was necessary in VB6 because of default properties. To tell VB that we want to assign a variable to the object itself (as opposed to the value of the object's default property) you had to use the Set keyword. For example, in VB6:

    Dim x as Variant
    Dim strName as string
    
    'Assign the value of Text1.Text to StrName
    '(Text is the default property of the textbox control in VB6)
    StrName=Text1      
    
    'Here we assign the actual Textbox object to the variable x
    'Note that we use the Set keyword so VB knows we want to assign
    'to x the object itself instead of the default property
    Set x=Text1
    

    In VB.NET, however, there are no default properties allowed (except for parameterized properties) and hence there is no need for the Set keyword. (Likewise, the Let keyword has been removed from the VB.NET syntax.)

    Error Handling
    Visual Basic has finally incorporated structured error handling. The keywords Try, Catch and Finally make error handling easy and make VB.NET at par with languages like C++ or C#. This Try ... Catch model allows developers to place code that may cause an exception within a Try block. If that code throws an exception (synonymous to raising an error), the code in the Catch block is executed; the code in this block should be designed to gracefully handle the exception.

    Note that VB6's older error handling techniques (On Error Resume Next and the like) are still supported for backward compatibility, although when writing new applications with VB.NET you should valiantly strive not to use these old techniques. The following code snippet illustrates various error handling techniques with VB.NET:

    Try
      ...
    Catch
      ...
    End Try
    

    The above code simply "catches" any exceptions caused by offending code inside the associated Try block. VB.NET allows you to handle specific exceptions by using multiple Catch blocks:

    Try
      ...
    Catch e1 as NullPointerException
      ...
    Catch e2 as Exception
      ...
    End Try
    

    In addition to catching predefined exceptions you can also create your own custom exception classes that inherit from the System.Exception base class. You can also 'Throw' your own exceptions (similar to using the Raise method of the Err object in VB6:

    If myvar < 1000 then
      Throw new Exception("Business Logic Error")
    End If
    

    In Part 3 we'll conclude our examination of VB.NET's new features.

  • Read Part 3!


  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article