473,394 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Inefficiency with arrays?

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
Nov 20 '05 #1
4 886
"Robin Tucker" <id*************************@reallyidont.com> schrieb
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


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

Nov 20 '05 #2
* "Armin Zingler" <az*******@freenet.de> scripsit:
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


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


'myObj' is a property (really stupid name, should read 'MyObj'). If the
property returns a copy of an array, the code will create copies of the
array ;-).

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
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

Nov 20 '05 #4
Ok, I see this now.

thanks.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:O4**************@TK2MSFTNGP09.phx.gbl...
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


Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
5
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
6
by: Robert Bravery | last post by:
Hi all, Can some one show me how to achieve a cross product of arrays. So that if I had two arrays (could be any number) with three elements in each (once again could be any number) I would get:...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
334
by: Antoninus Twink | last post by:
The function below is from Richard HeathField's fgetline program. For some reason, it makes three passes through the string (a strlen(), a strcpy() then another pass to change dots) when two would...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
29
weaknessforcats
by: weaknessforcats | last post by:
Arrays Revealed Introduction Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with arrays and array syntax but is not clear on a )exactly how...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.