文章概要:
这篇的文章的大意是,对于 VB.NET (VB200, VB2005, VB2008) 代码,在选择传递参数的方式时,简单起见,请都采用 ByVal 方式,不管你的参数是 Value type 或是 Reference type (参见值类型(Value Types)和引用类型(Reference Types)的区别)。
只有当该参数是 Reference type,而且你又需要修改参数本身,你才需要用到 ByRef,但这种情况比较少。
As you know, data types in the .NET Framework come in two varieties: value types and reference types. Likewise, you can pass any argument to a subroutine or function using the ByVal or ByRef keyword. In both cases, the term value implies that you are working with the data and the term reference implies that you are working with a pointer (or reference) to the data.
So, when you pass arguments to a subroutine or function, you absolutely must know whether the type is a value type or a reference type. Only then can you determine whether you want to use ByVal or ByRef to pass the data. Why? Well, let me answer this question with several of my own: Can you pass a value type by reference? What if you pass a reference type by value? Or a reference type by reference? As you can see, this can get pretty messy, pretty fast.
To help clear things up, this tip enumerates all the possibilities and describes what's actually happening behind the scenes in each situation. Once you understand that, you'll begin to see what the best approaches are for various situations.
So, here are the possibilities:
So, the upshot is you can modify the data in a reference type passed by reference. But that's no surprise -- you could modify the data in a reference type passed by value. Because of this and because of the unnecessary additional complexity incurred when using pointers to pointers, you should typically avoid this situation altogether. In other words, typically, you should always pass reference variables by value. Just be aware that they can be modified!
That covers most of the common scenarios. There is one more, less-common, scenario where the difference between a reference type passed by value vs. a reference type passed by reference actually makes a difference.
Suppose you pass a reference type by value to a subroutine and that subroutine sets the reference to NULL. Will the reference variable back in the calling routine also be set to NULL? Actually, no. When a reference type is passed by value, you can change the data, but you cannot change the reference itself. (Remember, the reference itself was passed by value.) However, if you pass the reference variable by reference, you can, in fact, set it to NULL and have that reflected back in the calling routine. This circumstance is the rare instance when passing a reference variable by reference would be necessary.
Whew! I know this can be confusing, but the takeaways are pretty straightforward:
Finally, if you have projects that have been converted from VB 6 to VB.NET, remember that the default in VB 6 was ByRef, whereas in VB.NET it is ByVal. When using automated conversion software, typically all parameters are specified as ByRef. This means that you are likely to find many instances of reference types passed by reference. While this won't cause your code to fail, it is bad programming practice and creates less efficient IL code. You should make a sweep through your code and convert any passed reference variables to ByVal.
Previous: ByVal (By Value) 和 ByRef (By Reference) 的区别 Next: 当参数是 Reference type 时使用 ByVal 和 ByRef 的区别
Parent: VB.NET 的参数传递机制
© 2008-2010 HubaPo.com 版权所有 Contact me