Robin,
Unfortunately that page only lists half of the example. They do not show you
have the MyObj property itself is implemented.
Proper OO encapsulation suggests that you do not return an private array
field from a public property, instead you return a copy of the array. This
prevents any one who is using your class from messing with the internals of
your class. Also it allows you to hide the implementation (internals) of you
class from any one consuming your class. The above rule is for untyped
collections such as an array itself. The rule does not apply to strongly
typed collections that are derived from CollectionBase or DictionaryBase...
Knowing this, it would suggest that the MyObj property is implemented
something like:
Public Class Class1
Private m_myObj() As Object
Public Readonly Property MyObj As Object()
Get
Return DirectCast(myObj.Clone, Object())
End Get
End Property
End Class
Note the Clone method above:
Dim obj As New Class1
Dim i As Integer
For i = 0 To obj.myObj.Count - 1
DoSomething(obj.myObj(i))
Next i
The example will now cause copies of the array as the statement indicates.
Hope this helps
Jay
"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c6*******************@news.demon.co.uk... In the MSN article
http://msdn.microsoft.com/library/de...guidelines.asp, they state that "each call to the myObj property creates a copy of the
array. As a result, 2n+1 copies of the array will be created in the
following loop." I cannot understand why this is - surely obj.myObj(i) is
just fetching a single value (at i) in the array obj.myObj.
Anyone see what I'm not understanding here?
Dim i As Integer
For i = 0 To obj.myObj.Count - 1
DoSomething(obj.myObj(i))
Next i