References in VB.NET

后端 未结 6 1931
無奈伤痛
無奈伤痛 2021-01-06 19:05

Somewhat unclear to me are references (pointers?) to classes in VB.NET. The question I am about to ask can be answered by a little bit of testing, but I was wondering if any

6条回答
  •  长发绾君心
    2021-01-06 19:28

    From MSDN:

    If you pass a variable argument by value using the ByVal keyword, the procedure cannot modify the variable itself. However, if the argument is a reference type, you can modify the members of the object to which it points, even though you cannot replace the object itself. In particular, you can modify the members of the object. For example, if the argument is an array variable, you cannot assign a new array to it, but you can change one or more of its elements. The changed elements are reflected in the underlying array variable in the calling code.

    Since your ReferenceClass is a reference type, if you pass it ByVal, you can't replace it with a new object (which you don't), but you can muck around with its innards (which you do). Whether you pass ByRef or ByVal, mucking around with its innards will still "affect" the original object (since there is only ever one object in memory).

提交回复
热议问题