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

Source: Internet
Author: User
Tags define definition class definition comparison inheritance instance method integer thread
Vbscript|visual
New easy to set (Assignment) syntax
Visual Basic.NET provides new, simplified syntax, when the code is written as follows
Myval = 10
Myval + 10
The value of this myval is 20, and the code is equal to
Myval = Myval + 10

Inherit (inheritance)
Visual Basic.NET is now a complete object-oriented (object Orient) language, which is the way to complete support for encapsulation, inheritance, polymorphism, etc., and to provide construction/Shijiko. So it adds to the previous functionality that Visual Basic lacks in this respect, such as inheritance.
The addition of Visual Basic.NET allows you to create new classes based on other classes, and the derived (Derived) class can inherit (inherit) and extend the attributes and methods of the original class. Although the derived class can only inherit from a single base class, it can actually do (implement) an infinite number of object interfaces (interface). The derived class can also write new methods override inherited methods, and all the class presets created by Visual Basic.NET can be inherited.
The inheritance and interface allow you to do multiple (polymorphism) so that different classes can redefine the attributes and methods of the same name. Polymorphism is the foundation of an object-directed (object-orient) program because it allows you to call a method of the same name, regardless of which type of object is used. For example, to give a Normalpayroll class, a variety of designers can define many different payemployee methods of derived classes. Other attributes and methods can use the Payemployee method of derivative parts in the same way, regardless of which derivative object is used.
Visual Basic.NET provides the following words to define and support the inheritance
    • Inherits the class (also called the base class) in which class is to be inherited. Can usually be set through a Properties window.
    • NotInheritable--Prevents the user from using the class as the base class
    • MustInherit-Defining that class cannot be used to build instance, the only way to use it is to inherit it.

The derived class can be a new way of override some of the methods that are inherited. Visual Basic.NET uses the following definitions to control the method and the overriding of the attributes
    • Overridable--Let the attributes and methods in the inheritance of Class can override
    • Overrides--Defining the nature or method of which foundation to override
    • NotOverridable (Preset)--class override to prevent belonging or methods from being inherited
    • MustOverride--requires that the class of inheritance must be override or method

Code examples such as the lower Imports System

Namespace MyNamespace Class Payroll
Overridable Function Payemployee (ByVal hoursworked as single, ByVal payrate as single)
Payemployee = hoursworked * payrate
End Function
End Class class BonusPayroll
Inherits Payroll
Overrides Function Payemployee (ByVal hoursworked as single, ByVal payrate as single)
Payemployee = (hoursworked * payrate) * 1.45 ' 45% Red Lee
End Function
End Class Module
MyModule Sub Main ()
Dim Bonuspayrollitem as BonusPayroll = New BonusPayroll
Dim Payrollitem as Payroll = New Payroll
Dim payrate as Single = 14.75
Dim hoursworked as Single = 40


Console.WriteLine ("Normal Pay is:" & Pay (Payrollitem, hoursworked, payrate))
Console.WriteLine ("Pay with bonus are:" & Pay (Bonuspayrollitem, hoursworked, payrate))


End Sub Function Pay (ByVal payobject as Payroll, ByVal hoursworked as single, ByVal
PayRate as single) Pay = Payobject.payemployee (hoursworked, payrate) * 0.75 ' withold tax
End Function
End Module
End Namespace The results are as follows in the code you can see class bonuspayroll inherit from Payroll and Override payemployee function. So when the Pay function is transferred to different Class objects but calls the same method, there can be different results. So the above program example also shows the use of multiple forms.

Free Threading
In previous versions of VB, developers can only do synchronous actions, that is, the code must be executed sequentially, unless they are able to develop multiple threading applications by using the Win API. Visual Basic.NET allows you to write multiple task applications. Each Task can be performed in a thread, and the program (process) can be free thread. Code range such as lower Imports system.threading

...

Private ival, Iary (1) as Integer

...

Protected Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs)
Dim T as New Thread (AddressOf addval)
T.start ()
End Sub
Sub Addval ()
SyncLock iary
Dim I as Integer
For i = 1 to 1000000
Ival + 1
Next TextBox1.Text = CStr (ival)
End SyncLock
End Sub

Constructs (constructor) and Shijiko (destructor) are the initialization procedures that are needed to control Class in creating a new Instance. In the same way, Shijiko is the program that needs to be done when the Instance of Class is finished (possibly setting variables to nothing), such as releasing the system resources, and so on. In the original VB, you can use the Class_Initialize () and Class_Terminate () event Procedure to initialize or at the end of the Instance.


In Visual Basic.NET, the Sub New () and sub Destruct () programs are used to make constructs and Shijiko actions.


The Sub New () method is only performed once in class creation, and cannot be called anywhere else except with the first travel code call of the same class of other constructs or derived Class constructs. And the code inside the Sub New () method is executed, no member of that Class has been created. The. NET environment will automatically call its Sub destruct () method when it is cleared of objects, nor can this function be used by the call.


The. NET environment when the system decides that the object does not need to exist, the object is automatically erased. Unlike Class_Terminate and Sub New (), you can't know for sure when the. NET dynamic Environment calls Sub Destruct (), only to make sure that after the last reference to the object (reference) is released, the system automatically calls Sub De struct ().

Use Sub New () to create a parameterized construct--a constructor that creates a class can write sub New () functions anywhere within the class definition. The first travel code of a construction must be to call the base class's construct or the other constructs within the same class, which guarantees that the object being inherited will be initialized before the derived object. Even when you build your own base class, you usually call MyBase.New to initialize more fundamental objects, because all classes are eventually inherited from object class.


After the parent object's construction is called, the action required to initialize the object is added to the sub New () secondary function. The Sub New () function can accept parameters, as in the following code example.

Using sub Destruct () as a solution--to establish class Shijiko, you can add sub destruct () to any place within the definition of class. You can add things to the letter, close the file, and so on.
Code examples such as the following
Imports System


Namespace MyNamespace


Module MyModule
Class Truck
Private Iwheels as Integer
Sub New (iinitialwheels as Integer)
MyBase.New ' Call base Class's construction
Iwheels=iinitialwheels
End Sub
Sub Destruct ()
' Perform some of the necessary actions
End Sub
Property Wheels as Integer
Get
Wheels=iwheels
End Get
Set
Iwheels = Value
End Set
End Property
End Class


Sub Main ()
Dim T as Truck = New Truck (18)
Console.WriteLine ("This is a {0} wheel Truck", T.wheels)
Console.Write ("Please input the number of wheels you want to change:")
T.wheels = CInt (Console.ReadLine ())
Console.WriteLine ("You've changed it to" & T.wheels & "Wheel Truck")
End Sub


End Module


End Namespace The results are as follows

Delegatedelegates acts like a type-safe, an object-directed function (object-oriented function pointer). Using the AddressOf operator of VB to obtain the reference of the class delegate to the other VB class auxiliary function. You usually use Delegate to do event processing functions, which are the functions that you want to perform automatically when an event happens.


Each delegate class defines a construct, which is used to refer to an object method as a reference. The format of the parameters is as follows
AddressOf [.] < method name >
During the translation time, it must be a class or an interface (interface) and have a method that conforms to the format of the method declared by delegate class.
This method can be either a shared (class) method or a instance method. The method to delegate class instance must call the built-in Invoke method, and the entire code example
Imports System
Namespace testdelegate
Public Delegate Sub EventHandler (ByVal strText as String)


Class Eventtake
Sub GetEvent (ByVal Strin as String)
Console. WriteLine ("Delegate event" & Strin)
End Sub
End Class


Class eventraise
Sub Useinvoke ()
Dim C as New Eventtake ()
Dim Delsub as EventHandler
Delsub = New EventHandler (AddressOf c.getevent)
Delsub. Invoke ("Some situations")
End Sub
End Class


Module Module1


Shared Sub Main ()
Dim C2 as New eventraise ()
C2. Useinvoke ()
End Sub


End Module


End Namespace The results are as follows

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.