Inefficiency with arrays?

R

Robin Tucker

In the MSN article
http://msdn.microsoft.com/library/d...s/cpgenref/html/cpconarrayusageguidelines.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
 
A

Armin Zingler

Robin Tucker said:
In the MSN article
http://msdn.microsoft.com/library/d...s/cpgenref/html/cpconarrayusageguidelines.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

I also don't see why I copy of the array is made. I think the statement is
wrong.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
J

Jay B. Harlow [MVP - Outlook]

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
 
R

Robin Tucker

Ok, I see this now.

thanks.

Jay B. Harlow said:
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

The example will now cause copies of the array as the statement indicates.

Hope this helps
Jay


http://msdn.microsoft.com/library/d...s/cpgenref/html/cpconarrayusageguidelines.asp,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top