繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> Asp.Net开发 >> Chapter 3 Major VB.NET Changes(2)

Chapter 3 Major VB.NET Changes(2)

2007-06-15 08:00:00  作者:  来源:互联网  浏览次数:0  文字大小:【】【】【
简介:Dim txtBillTo as TextBox Dim txtShipTo as TextBox txtShipTo =txtBillTo The line of code txtShipTo =txtBillTo sets the Text property of txtShipTo to the value in the Text property of txtBillTo . Bu...
关键字:Changes Chapter Major NET VB

Dim txtBillTo as TextBox

Dim txtShipTo as TextBox

txtShipTo =txtBillTo

The line of code txtShipTo =txtBillTo sets the Text property of txtShipTo to the

value in the Text property of txtBillTo . But what if that isn’t what you wanted?

What if, instead, you wanted to create an object reference in txtShipTo that referred

to the object txtBillTo ? You’d have to use this code:

Set txtShipTo =txtBillTo

As you can see, default properties require you to use the Set keyword to set refer-ences

from one object variable to another.

VB.NET gets around this problem by getting rid of default properties. Therefore, to

copy the Text property from txtBillTo into the Text property of txtShipTo ,you’d

have to use this code:

txtShipTo.Text =txtBillTo.Text

Setting the two variables equal to each other sets a reference from one to the other. In

other words, you can set an object reference without the Set keyword:

txtShipTo =txtBillTo ‘ Object reference in VB..NET

To be more precise, default properties without parameters are no longer supported.

Default properties that require parameters are still valid. Default properties with para-meters

are most common with collection classes, such as in ADO. In an ADO exam-ple,

if you assume that rs is an ADO Recordset, check out the following code:

rs.Fields.Item(x).Value ‘ OK,,fully qualified

rs.Fields(x).Value ‘ OK,,because Item is parameterized

rs.Fields(x)‘ Error,,because Value is not parameterized

The easy solution is to fully qualify everything. This avoids any confusion about

which properties are parameterized and which are not. However, as you know from

your VB days, the number of dots you have should be minimized. The reason is that

each dot requires an OLE lookup that slows you down. Therefore, you should code

carefully when dealing with parameters.

Subs and Functions Require Parentheses

As you saw in the last chapter when you used the MsgBox function, you must now

always use parentheses with functions, even if you are ignoring the return value. In

addition, you must use parentheses when calling subs, which you did not do in VB6.

For example, assume that you have this sub in both VB6 and VB.NET:

Sub foo(ByVal Greeting As String)

‘ implementation code here

End Sub

责任编辑:admin
相关文章