Comparison of Visual Basic 6/vbscript with Visual Basic.NET (medium)

Source: Internet
Author: User
Tags filter definition comparison contains exception handling integer variable scope visual studio
Vbscript|visual
Variable range (Variable Scope) Visual Basic.NET allows the local variable to support the block range, that is, the variables declared in the loop or If, and so on, are not visible externally, so if the code writes the following imports System


Namespace MyNamespace


Module MyModule
Sub Main ()
Dim J As Integer
For J=1 to 10
If True Then
Dim I as Integer
I=i+1
Console.WriteLine ("I= in the Interior" & CStr (i))
End If
Console.WriteLine ("Whether to use i= in the interior" & CStr (i))
Next J
End Sub


End Module


End Namespace
Using the variable i in the If area will cause the translation error, and the following diagram can be correctly executed if it is taken off the line.

Note, though, that the variable is not visible outside the block, but the life cycle of the variable is not lifetime. Which means that if you go back into the block, you can still use the variable. So the code translated after this is done as follows

Structured exception processing (structured Exception handling) Visual Basic.NET support structured exception processing, using other languages such as C + + already used in the language Try ... Catch... Finally, a protected program area (protected blocks) is used with filter (filter) to make exceptions. Use Original on Error ... This unstructured exception is inefficient, and it is difficult to protect the code.

Try ... Catch... Finally the code structure is as follows--try ' starts to construct the exception processing, the code in this section may produce an exception Catch [filtering filter] ' If there is an exception in the Try section, it will execute the code [other Catch block] in this area Finall Y ' must execute the code end try before leaving the try Block
There are exceptions to the code in the Try region that need to be monitored. If any code that is executed in this section is wrong, the license is sent to the first line in the Catch area. In a Catch block, you can place a code that has a general exception (bug). You can define a number of catch areas and define different catch areas in different situations. In the Finally area, you can place the end of the code, such as close the file, release the object, and so on.

Use Try ... Catch... Finally--Use a Try ... Catch... Finally The block can be wrapped around a potentially wrong code, and you can use the nesting (nest) method to wrap exceptions in the exception processing, and the variable declared in each block is the area of the region variable.
Programs such as the following

Function Getstringsfromfile (ByVal FileName As String) as String
Dim strtest, Strings as String
Dim Stream as StreamReader = File.OpenText (FileName) ' Open file '


Try
While True ' Loop has been performed to endofstreamexception error
Strtest = Stream.readline ()
If strtest = "" Then
Throw New EndOfStreamException
End If
Strings = Strings + strtest
End While
Catch Eofexcep as EndOfStreamException
' No need to do anything, has reached the end of the file
Catch Ioexcep as IOException
' Some bugs have happened, back to the wrong
MsgBox (Ioexcep.message)
Strings = Nothing
Finally
Stream.Close () ' Close file
End Try
Return Strings
End Function


Using the correct data type
In defining subroutines and functions, you can use the information provided by VB to define the parameters, and the type of function return
Sub dosomething (ByVal strValue1 As String, ByVal intcount as Integer, ByRef strValue2 As String = "initial content")
Function dosomething (ByVal strValue1 As String, ByVal intcount as Integer, ByRef strValue2 As String = "initial contents") As String
You can use the Optional key to define the number of input parameters, but because Visual Basic.NET no longer support the IsMissing keyword, you must give the preset value.
Function dosomething (ByVal strValue1 As String, ByVal intcount as Integer, ByRef strValue2 As String = "initial content"
_ Optional ByVal strtype = "Preset contents As string" As String
Integer and long integer data type change--integer (integer) now is bits instead of previous bits, the length is now in the bits instead of the bits. This makes it easier for VB to collaborate with other languages, especially when calling APIs. Call APIs tend to pay attention to the length of bits, so you should pay special attention to the changes in VB's definition of data type.

Transform (Cast) to the correct data type-Visual Basic.NET requires the right data type, so when you change the data type, use the appropriate function, and the list follows the name of the returned data type CBool (value) Boolean CByte ( Value) Bytecchar (value) Char CDate (value) Datecdec (value) decimalcdbl (value) Double CInt (value) integerclng (value) Longcobj (value) objectcshort (value) short CSng (value) singlecstr (value) String

The user-customized data structure (Structure) uses the keyword Structure in Visual Basic.NET to define the user-defined variable structure (user-defined type UDT) and does not support VB6 using the type key.
Code range such as Structure ABC
Public UserName as String
Public UserId as Integer
Dim Userage as Integer
End Structure

...


Dim EMP as ABC
Emp. Userage = 30
Emp. UserName = "Hu Baijing"
Emp. UserId = 12345 The members in the structure must define access ranges such as public, Private, or Protected. You can also use the Dim phrase, which has a preset value of public access.
< Visual Basic.NET new and VB6 no part > The following is a pure new feature of Visual Basic.NET that was not available in visual Basic in the previous version. Content has
    • Assemblies
    • New easy to set (Assignment) syntax
    • Inherit (inheritance)
    • Free Threading
    • Construction (constructor) and Shijiko (destructor)
    • Delegate

Assemblies Assembly is an application that is composed of the. NET platform. It is a unit of installed and scattered applications on the. NET environment, and exists as an. exe or an action-connected library (. dll). You use the content of assemblies in Visual Basic.NET, and you add a reference to assemblies as if you were using the Type library in a previous version of VB. The difference between assembly and a previous. exe or. dll file is that it contains all the scheduled information, including the type library and the related components that the program uses, and so on.
The assembly contains a assembly manifest, a bit like a list of all the contents of a assembly, containing information as follows: the identity of the Assembly, such as his name, version, etc. used to describe a list of all the files in the assembly, Contains other assembly that you have created for your assembly (. exe or. dll), as well as graphics (bitmap) or Readme files, and so on.
A assembly reference (reference), which is the list of all dependent external files-DLLs or other files your application needs, but not you. Assembly's participation included a reference to a public (global) or private object. Public objects exist in the public assembly cache, which is where other applications can be accessed, a bit like System32. Microsoft.VisualBasic is an example of a public assembly fast-access area. A private object must exist in your program to install the same catalogue or its subdirectories.
Because assemblies's self-describing information is sufficient, the application of Visaul Basic.NET no longer requires regisry information. This reduces the DLL's conflict and allows your application to be more stable when installed. In most cases, an installation of. NET applications can be made only by copying files into the target computer.

Use assemblies-to use assemblies you have to add a reference to it and then use the Imports language to select the name space (namespace) of the object you want to use. Once the reference and imports are complete, your application can use all the classes, attributes, methods, and other members of the same named space. A single assembly can contain more than one named space, and there are multiple items under each named space.
Within Visual Studio.NET, you can build assembly by translating the application. With Visual Basic.NET you can build an. exe or. dll file at the same time, and use other languages that support common language definition (Common Language specification).

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.