Vb. NET basic syntax

Source: Internet
Author: User
Tags constructor error handling new features numeric value valid versions

For a long time, VB has been ridiculed by some people because of its lack of perfect object-oriented support, lack of efficient error handling mechanism and poor performance. Vb. NET will completely change this situation. However, VB. NET in these aspects of the improvement also pay the price, many of the old code needs to manually convert to run under vb.net.

The following tables summarize the grammatical changes of the vb.net language. Note that these tables do not fully list all changes, but list some of the most important changes.

Table A contrasts some of the familiar grammatical forms in VB6 and their closest grammatical form in vb.net.

Table A: Grammar contrast old syntax new syntax description Form Mount event, class initialization event Sub New procedure Sub New is called constructor Method (constructor), it can have parameters.

The Property Let property Set let keyword is no longer valid.

Currency decimal in VB6, Decimal is a variant of the seed type, but in the. NET, it is an intrinsic type of data. NET no longer supports currency data types.

Variant Object VB. NET's object data type has the ability to VB6 object types and Variant types.

Debug.Print Debug.Write Debug.WriteLine This change is just a simple change in name: from Print to Write and WriteLine. Wend End While VB. NET recommends using a while loop rather than a Do loop.

In order to allow VB data types and other. NET language, Microsoft modifies the representation of the integer class data type and adds a new data type. These changes are especially important for methods that make external calls (such as API calls). For example, if the called function requires a 32-bit integer argument, it should be declared as long in VB6 and should be declared as Integer in vb.net.

Table B: And integer-related data type length VB6 and earlier versions of the name VB. NET bit integer short bit long integer bit (none) long

In vb.net, Microsoft reduced many of the keywords originally used for VB6 and replaced them with "framework classes." The reason for this substitution is that the functionality in the Framework class is for all. NET language is valid. The following table lists some of the affected keywords.

Table C: Replaced keywords

vb keyword VB. The location method/attribute in the net namespace

Circle System.Drawing.Graphics DrawEllipse

Line System.Drawing.Graphics DrawLine

Atn System.Math Atan

SGN System.Math Sign

SQR System.Math Sqrt

Rnd Microsoft.VisualBasic.Compatibility.VB6 Rnd

Round Microsoft.VisualBasic.Compatibility.VB6 Round

Lset System.String PadRight

Rset System.String PadLeft

DoEvents System.Winforms.Application DoEvents

VarType System.Object GetType (returns the object of the class type that contains the properties that can extract the information)

In vb.net, variables, array declarations, and initialization methods have changed, and the following table outlines the changes in vb.net in this regard.

Table D: New declaration methods

Examples of change syntax

Multiple types cannot be declared in a single declaration statement. ' VB. NET does not allow the following declaration to appear!

Dim ncount as Integer, banswer as Boolean

When declaring a variable, you can give the initial value Dim ncount as Integer = 20

Dim Ndoublecount as Integer = ncount * 2

Allow an array element to specify an initial value Dim nindex (3) as Integer = (3, 5, 7)

Cannot be declared with ReDim, only the size of the array can be redefined with ReDim. ' The following line of code is illegal in vb.net!

Redim sname () as String

A lot of new keywords implement the new features in vb.net. Here are some of the most important keywords and their use, usage, simple examples.

Table E:vb.net's New keyword

Simple examples of keyword use

Inherits points to the base class, which is used to implement inheritance. Inherits System.WinForms.Form

MyBase in the code of the subclass, MyBase references the base class. Stringproperty = Mybase.stringproperty

Shared shared represents a variable within a class for all instances of the class. Public Shared Baselocation as String

Try

Catch

Finally

Throw This is the new error-handling keyword. Try begins a block of code that enables error control, and catch identifies a block of code that handles a particular error, and finally begins a block of code that must be executed regardless of whether the error occurred, throw throws an error (similar to VB6 's Err.Raise). Try

Rsrecordset.update

Catch

Logerror ("Update failed!")

Finally

Rsrecordset.movenext

End Try

ReadOnly in the property declaration, ReadOnly indicates a read-only property (only the properties of the Get procedure). Public ReadOnly Property

Stringproperty () as String

WriteOnly in the property declaration, WriteOnly indicates a property that can be written only (the properties of the set procedure only). Public WriteOnly Property

Stringproperty () as String

Char This is the single character data type in the vb.net. Dim Chrinitial as Char

Imports introduces the specified class in the current code module. Imports system.winforms

NAMESPACE Specifies the namespace (Namespace) for the module. Namespace Myapplicationname

Overloads overload. Overloads represents the implementation of multiple versions of the same function name, which the compiler distinguishes from the function's argument list. ' The following declarations are included within the same module ...

Overloads Sub Display (sIn as String)

Overloads Sub Display (nIn as Long)

Overrides covered. Overrides represents a member function that overrides the specified method in the base class that the current class inherits. Inherits MyBaseClass

Overrides Function Name (NID as Long) _

As String

Overridable Overridable indicates that any class that inherits from the current class can override the specified member function. Overridable Function Name _

(NID as Long) As String

MustOverride MustOverride indicates that any class that inherits from the current class must overwrite the specified member function. MustOverride Function Name _

(NID as Long) As String

Protected Protected indicates that a member function can only be accessed from a derived class of the current class. Protected Sub Clear ()

In the previous VB, until VB 4, let, set and get property process is separate. Vb. NET puts the property process of the same attribute together:

Private Msmystringproperty as String

' Declaration area

Public Property Mystringproperty as String

Get

Mystringproperty = Msmystringproperty

End Get

Set

Msmystringproperty = Mystringproperty

End Set

End Property

Vb. NET no longer has a let property procedure, because the syntax of all assignment statements, whether object or not, is the same.

Language changes are far more than changes to the architecture. These changes make sense for most people, but there are still some objections to some changes. For example, in previous releases, many tasks could be done in a number of different ways, and the unified coding standard either did not exist or was difficult to implement. To "clean up" the VB language, Microsoft has made some major changes to VB, and many of the tasks that previously could have multiple methods of implementation now have only one method.

In addition to what is listed in the previous tables, here are some of the areas that are particularly important to note.

First, the default way to pass data to a procedure parameter is to pass the original pass-through reference (BYREF) to the passing value (ByVal), which is an important change. Passing parameters by reference is more dangerous than passing parameters by value, which is the danger that the invoked procedure may inadvertently change the value of the parameter. Vb. NET still allows parameters to be passed by reference, but the change in the way the default parameters are passed means that the program must be adjusted accordingly.

Second, VB. NET no longer has a set statement, assigning an object reference to a variable now takes only an equal sign, and the object can be treated like any other value. While omitting the set simplifies the code, there is an attendant effect: The default property is no longer valid. For example, the following property value reference method is no longer valid:

Text1 = "This is the object's default property value. "

Instead, the property value must be explicitly referenced as follows:

Text1.Text = "This is the object's default property value. "

On the face of it, VB. NET does not seem necessary to make such a request. But in fact, it is necessary to get rid of the default attributes. For example, suppose you have an object variable named Objfoo, and since there is no SET statement, if the attribute value can still be referenced as it was, it is difficult to determine what the following statement means:

Objfoo = Text1

Does this statement set a reference to the TEXT1, or do you assign the value of the Text1 Text property to Objfoo? We cannot make judgments, and the compiler is equally incapable of judgment. Therefore, discarding the SET statement also means that the default property value must be discarded.

。 The least-liked change in net: Microsoft has changed the meaning of some data types that have long been in use. In. NET, the integer becomes 32 digits, and long is 64 bits. It can be imagined that this change will lead programmers to use the wrong variable type frequently. For example, should an API function be invoked with a 16-bit integer or a 32-bit integer? Hopefully, Microsoft will reconsider this decision with some new variable type names, such as Int32 and Long64.

VB. NET introduced the Option Strict keyword. Option Strict keyword is used instead of option Explicit. The original VB allows a numeric value to be assigned to a string variable, or other similar abnormal assignment operations, Option Strict end this situation. The declaration Option Strict tells Visual Basic.NET not to make any coerced type conversions. Of course, VB. NET does not fully restrict type conversions: It allows for downward automatic type conversion (CAST), but does not support upward automatic type conversions. For example, if an explicit type conversion is not made using a statement such as "sngvariable = CSng (dblvariable)", a variable declared as single cannot be set to the value of a double variable because it can result in data loss; The double variable can be set directly to the value of a single type variable without explicit type conversion because there is no data loss problem. Using Option Strict can help developers reduce many errors, including many errors that are difficult to debug. Accompanying note: Deferred binding is not allowed to be used when Option Strict is used.

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.