Vb. One of the introduction of net programming (rotate)

Source: Internet
Author: User
Tags expression readable
Programming with previous bindings
Visual Basic 6.0 and Visual Basic.NET both support deferred binding of objects.
The method defines a variable to be an object data type beforehand and then sets it to a class at run time. However, during the upgrade process, you may have an error accessing the default properties of the deferred bound object. For example, suppose the project contains a label object named Label1 in the Form1,form1, and the following Visual Basic 6.0 code will label the caption
Set to "Sometext"

Dim O as Object
Set o = Me.label1
O.caption = "Sometext"

In Visual Basic.NET Windows Forms, the caption of a Label control
Sex is called the Text property. When your code is upgraded, the Caption property in all instances is converted to the Text property, but the deferred bound object is of no type (type-less), and VB cannot detect the type of object and make changes, in which case you need to manually make changes to the code. When you use a previously bound object, the object can be automatically upgraded as follows:

Dim O as Label
Set o = Me.label1
O.caption = "Sometext"

So in the program you need to define the object as the appropriate object type rather than simply
is defined as an object type.

Similar to Visual C + +. Visual Basic.NET supports overloading of functions, such as the following
A function of a plane has two shapes

Environ (Expression as Integer) as String
Environ (Expression As String) as String

Visual Basic.NET automatically detects the function that uses that parameter. If you pass
A positive value is passed to Environ (), then a positive call version is invoked, and if a string is passed, the string version is invoked. Passing an object or Variant data type to an overloaded function will result in a run-time error. If you want to produce the correct result, you need a cast of the data type, for example:

Dim A as String
Dim V as Variant
v = "Path"
A = Environ (CStr (v)) ' correct

It is a good programming practice to use coercion type conversions for deferred-bound objects.
This makes the code more readable and can be very easy to migrate to Visual Basic.NET. Use the date/time earlier version of VB support to store and change time using the double data type.
In Visual Basic.NET, this feature is no longer supported because the date inside the computer is not stored in double, for example, the following code can be run in Visual Basic 6.0, but an error occurs in Visual Basic.NET.

Dim Dbl as Double
Dim dat as Date
DAT = Now
DBL = Dat ' ERROR, cannot assign a date variable to a double variable
Dbl = DateAdd ("D", 1, Dbl) ' ERROR, double variable cannot be used for date letter
Number
DAT = CDate (dbl) ' ERROR CDate function cannot convert double variable to date

. NET architecture provides the tooadate and fromoadate functions used in a double variable to
and date variables. However, this needs to be manually modified after the upgrade, so you should use the date data type when storing dates.

Default Properties
In Visual Basic 6.0, many objects have default properties, and the default properties are
The code can be omitted. For example:

MsgBox Form1.Text1.Text

You can write this:

MsgBox Form1.text1

The default property is determined in code compilation. Similarly, you can also bind objects in a deferred way so that the
With default properties, such as:

Dim obj as Object
Set obj = Form1.text1
MsgBox obj

For deferred-bound objects, the default properties are determined when the program is run.

Visual Basic.NET does not support default properties, and when your project is upgraded,
Visual Basic.NET automatically determines the default properties, but it is not possible to automatically resolve the problem of determining default properties for deferred-bound objects. In this case, you need to modify the code yourself. More complicated, many libraries implement the default properties by _default properties. The _default image is a proxy that passes the call to the real lack of the provincial nature. So when your project is upgraded, some properties will be changed to _default. The code is still working as usual, but the code is less readable than the original actual property.

Dim obj as Object
Set obj = Me.text1
MsgBox obj ' bad code, using default properties
MsgBox me.text1 ' bad code, using default properties

Use

Dim obj as Object
Set obj = Me.text1
MsgBox obj. Text ' Good code, writes out the attribute
MsgBox Me.Text1.Text ' Good code, writes out the attribute

Although the default properties are not supported by Visual Basic.NET, the absence of a parameter
Sex is supported, to understand the difference between them, here is an example:

Dim rs as ADODB. Recordset
RS ("CompanyName") = "SomeCompany"
Rs! CompanyName = "SomeCompany"

Actually equals:

Dim rs as ADODB. Recordset
Rs. Fields ("CompanyName"). Value = "SomeCompany"
Rs. fields! Companyname.value = "SomeCompany"

Because value is the default property of fields for the second, Visual Basic.NET is supported; however, the first is not supported, and the correct usage is as follows:

Dim rs as ADODB. Recordset
RS ("CompanyName"). Value = "SomeCompany"
Rs! Companyname.value = "SomeCompany"

Examples like the above and most of the others can be solved in the event of a project upgrade, but
You will still want to avoid using the lack of a database in Visual Basic 6.0 for object and Variant data types. Because the upgrade program cannot discover and resolve these errors.


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.