A review of changes from VB6 to vb.net (turn)

Source: Internet
Author: User
Tags error handling integer variables valid variable scope
A review of the changes from VB6 to vb.net

When creating a asp.net page, you need to use a compatible one. NET programming language, such as C #, VB. NET or JScript.NET. But there are many important changes from Vb6/vbscript to vb.net, which is the content of this article.

Also, if you want to get more information about it, I strongly recommend that you read Microsoft's article: Prepare for the upgrade of Visual Basic 6.0 applications to Visual Basic.NET.

Introduction
With Microsoft. NET platform, Visual Basic is beginning to become a completely object-oriented language, referred to as vb.net. Although there have been some fundamental changes, the enthusiasm of developers from VB6 to vb.net is still quite high, even for experienced programmers. In this article, I'll list the changes in the Visual Basic language and illustrate them where appropriate. The list here may not be complete, but it already includes most of the functionality. If you are a VB programmer who wants to go up to vb.net, this article must be read.

The ASP.net Beta 1 version can be downloaded free of charge from http://www.asp.net/. Some of the changes discussed in this article are in Beta 2, and the new content in Beta 2 is not valid in the Beta 1 version of ASP.net, and is noted in the text.

Changes in data types
. NET platform provides a common type system for all supporting languages, which means that all languages must support the same data types that are enforced by the common language runtime environment, thus eliminating the incompatibility of data types between different languages. For example, on a 32-bit Windows platform, in languages such as C + +, the integer data type occupies 4 bytes, while in VB it occupies 2 bytes. The following are the data type-related changes in vb.net:

In. NET, the integer data type in vb.net is also 4 bytes.
Vb. NET has no currency data type, and as a substitute, it provides decimal.
VB.net introduces a new type of data, called Char. The char data type occupies 2 bytes and can store Unicode characters.
VB.net does not have a Variant data type. To get the same result as the variant type, you can use the object data type. Because in the. NET, including the original data type, is an object, a variable of type object, which can point to any data type.
There is no fixed-length string concept in vb.net.
In VB6, we use the TYPE keyword to declare the user's custom structure. Vb. NET introduces the same structure keyword for this purpose, and the other parts of the syntax are the same:
Structure MyStruct1
...
End Structure


Variable declaration
Now look at a simple VB6 example:

Dim x,y As Integer

In this example, VB6 X as a variant, and y as an integer. Vb. NET improves this interpretation by creating integers for both x and Y. In addition, vb.net allows you to assign an initial value to a variable in a declaration statement, as follows:

Dim str1 as string= "Hello"

VB.net also introduced a read-only (read-only) variable. A read-only variable, unlike a constant, does not have to be initialized when it is declared, but once you assign it a value, it cannot be modified. Take a look at the following example:

' Not initialized
Dim readonly x As Integer
' Attached value
x=100
' Can no longer be modified, so the following statement will have an error
x=200


Array
When using VB6, you can programmatically define the upper and lower bounds of the array. In vb.net, the lower bound of the array is always 0, which is what defines the array:

Dim Astates as String

The actual created element is 51, where 0 is the lower bound and 50 is the upper limit. Note: in vb.net Beta 1, the above statement creates 50 elements, and the upper and lower limits are 0 to 49.

Variable Range
Consider the 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 works very well in VB6 because it does not have a module-level variable scope. Module-level variables occur in other advanced programming languages, such as C + +. Variables defined in the Declaration module, as in the If ... The variables defined in the then module will fall out of scope when the declaration module ends. This way, if you define the If ... Accessing z outside of the then module can cause an error in the Advanced programming language. VB.net, in contrast to VB6, uses the variable scope of the module layer.


Set and let declarations
In VB6, you must assign an object to the variable using the set declaration. This is necessary in VB6 for the reason of the default property. To tell VB that you want to assign a variable to the object itself (as opposed to the default property value of the object), you must 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 ', the TextBox control in VB6)
Strname=text1
' Here we are assign the actual Textbox object to the variable x
' We use the ' Set keyword so VB knows we want to assign
' To X ' object itself instead of the ' default '
Set X=text1

However, in vb.net, the default property is not allowed (unless it is a parameterized property), so there is no need to use the SET keyword. Similarly, the Let keyword is removed from the vb.net syntax.

Error handling
Visual Basic ultimately combines structured error handling. Keyword try, catch, and finally make error handling simpler, and also allow vb.net to be tied up with C + + or C # languages. Try ... The catch pattern allows developers to place code that may cause exceptions in a try module. If the code does create an exception (synonymous with causing the error), execute the code in the Catch module, and the code in the module should be designed to handle the exception.

Note that in order to be backward-compatible, the old error-handling techniques (errors Resume Next and similar) in VB6 are supported, but you should bravely strive not to use these old technologies when writing new applications with vb.net. The following code snippets illustrate the various error-handling techniques for vb.net:

Try
...
Catch
...
End Try

The above code simply captures the exception caused by conflicting code in the associated try module. Vb. NET allows you to use multiple catch modules to handle special exceptions:

Try
...
Catch E1 as NullPointerException
...
Catch E2 as Exception
...
End Try

In addition to capturing predefined exceptions, you can also create your own custom exception classes, which are inherited from the System.Exception base class. You can also activate the custom exception, which is the same as the Raise method for the Err object in VB6:

If MyVar < 1000 Then
Throw new Exception ("Business Logic Error")
End If

static method
VB.net allows you to create static methods in the class. Static methods refer to methods that can be invoked without the need for a developer to create an example of a class. For example, if you have a class named Foo that has a non-static method nonstatic () and a static method static (), you can invoke the static () method:

Foo.static ()

However, non-static methods need to create an example of a class, like this:

' Create An instance of the ' Foo class
Dim Objfoo as New Foo ()
' Execute the nonstatic () method
Objfoo.nonstatic ()

To create a static method in a VB.net class, you only need to prefix the method definition with the Shared keyword.

Programs and Functions
In the default state, all program parameters in VB6 are passed by reference (BYREF), and in the default state of vb.net, these parameters are passed in value mode (ByVal). Regardless of whether the program or function receives parameters, parentheses are required when calling them. In VB6, the function return value uses this syntax: functionname = return_value. In vb.net, you can return a value by using the Returns keyword, returning return_value, or you can continue using the old syntax, which is still valid.

property syntax
In VB6, we use the Property Get and properties set/let to create attributes in the class, which appear in separate programs:

Public Property Get PropertyName () as DataType
...
End Property
Public Property Let PropertyName (value as DataType)
...
End Property


In vb.net, the property gets and the property let/set are merged into a property statement, not two separate statements. In addition, in the set section of a property statement, variable value refers to the value that is entered when the user assigns a value to the specified attribute.

public [readonly| WriteOnly] Property PropertyName as DataType
Get
Return M_var
End Get

Set
M_var = Value
End Set
End Property

Conclusion
Vb. NET has changed in semantics and syntax, and here are the most important changes that will be encountered as a asp.net developer. When you create a asp.net web page with vb.net, the most important points to keep in mind are:

Variables can be sorted, no longer variants. That is, if you need an integer variable, you should use Dim i as Integer, not just dim i. It is much better to classify a variable than a variable that is not categorized.
To keep in mind, VB. NET requires parentheses around the calling parameter when the subroutine is invoked. This means that Response.Write "Hello, world!" will generate errors, the correct one should be Response.Write ("Hello, world!").
VB.net no longer supports default properties, you must explicitly specify the attributes you want to access from an object.
When declaring an array, be aware that VB. NET, the lower limit of all arrays is 0, the upper limit is the number you specify.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.