Chapter 3 Major VB.NET Changes(2)
点击次数:62 次 发布日期:2008-11-06 08:07:07 作者:源代码网
|
源代码网推荐 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 软件开发网 www.mscto.com 源代码网推荐 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 源代码网推荐 源代码网推荐 源代码网供稿. |
