473,397 Members | 1,972 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,397 software developers and data experts.

Duplicating arrays

Is there an easier way to duplicate an array than copying each element in a
loop? If I just set one array equal to the other they end up pointing to the
same storage location and I can't manipulate one without effecting the
other.

Thanks for your help

Dave
Nov 21 '05 #1
7 1059
"Dave" <dave@4_rem_ove_scotts.com> schrieb:
Is there an easier way to duplicate an array than copying each element in
a loop? If I just set one array equal to the other they end up pointing to
the same storage location and I can't manipulate one without effecting the
other.


'Array.Copy' (shared member), 'Array.CopyTo' (instance member).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #2
Herfried,

I have a class set up with 2 arrays, one for the original data and one to
use for manipulating data. For example :

public class someData
public rawData as array
private manipData as array
public sub new(byval dt as array)
dt.CopyTo(rawData, 0)
dt.CopyTo(manipData, 0)
end sub
public function min() as double
array.sort(manipData)
return manipData(0)
end function
end class

But when I get to the array.sort(manipData) it modifies the rawData array as
well. I'm sure I've missed something simple.

Thanks again for your help.

Dave

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"Dave" <dave@4_rem_ove_scotts.com> schrieb:
Is there an easier way to duplicate an array than copying each element in
a loop? If I just set one array equal to the other they end up pointing
to the same storage location and I can't manipulate one without effecting
the other.


'Array.Copy' (shared member), 'Array.CopyTo' (instance member).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3
"Dave" <dave@4_rem_ove_scotts.com> schrieb:
I have a class set up with 2 arrays, one for the original data and one to
use for manipulating data. For example :

public class someData
public rawData as array
private manipData as array
public sub new(byval dt as array)
dt.CopyTo(rawData, 0)
dt.CopyTo(manipData, 0)
end sub
public function min() as double
array.sort(manipData)
return manipData(0)
end function
end class

But when I get to the array.sort(manipData) it modifies the rawData array
as well. I'm sure I've missed something simple.


I am not able to repro this behavior using the code below:

\\\
Dim Original() As Integer = {3, 6, 1, 9}
Dim Copy(Original.Length - 1) As Integer
Original.CopyTo(Original, 0)
Array.Sort(Original)
MsgBox(Original(0) = Copy(0))
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #4
I didn't know that way but did know of
Dim Array(,) as Integer
Dim Array2(,) as Integer = Array.clone()
this assumes that you are not using OPTION STRICT. In that case
you would need to use the following:

Array2 = DirectCast(Array.clone(), Integer(,))
The need for all of this is that in VB.Net Arrays are now reference
based instead of value based. So directly copying with just "="
doesn't work but creates a pointer to the new array.
This isn't completely my own ideas did get the info from MS Press:
Programming Microsoft Visual Basic .NET by Francesco Balena, etal.
Great book, would recommend to all. Great information, great examples,
makes you think but shows you the details. At over 1300 pgs I hope
to get through it soon. The above information though is expalined in
chapter 2 on datatypes and variables.

Frank Clark
"SBS Rocks"
1/4/2005 11:50:46 AM
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in
message
<#S**************@tk2msftngp13.phx.gbl>
"Dave" <dave@4_rem_ove_scotts.com> schrieb:
Is there an easier way to duplicate an array than copying each element in a loop? If I just set one array equal to the other they end up pointing to the same storage location and I can't manipulate one without effecting the other.


'Array.Copy' (shared member), 'Array.CopyTo' (instance member).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #5
Sorry,

Found my mistake. Array.CopyTo was what I needed. Thanks for your help
Herfried.

Dave

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"Dave" <dave@4_rem_ove_scotts.com> schrieb:
Is there an easier way to duplicate an array than copying each element in
a loop? If I just set one array equal to the other they end up pointing
to the same storage location and I can't manipulate one without effecting
the other.


'Array.Copy' (shared member), 'Array.CopyTo' (instance member).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #6
Is your third line correct? The one that reads:

Original.CopyTo(Original,0)
Shouldn't it read:

Original.CopyTo(Copy,0)

?

Nov 21 '05 #7
"Chris" <du******@gmail.com> schrieb:
Is your third line correct? The one that reads:

Original.CopyTo(Original,0)
Shouldn't it read:

Original.CopyTo(Copy,0)

?


Sorry, it should definitely read like you said...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #8

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

Similar topics

0
by: abu | last post by:
I'm having a problem with Fast Template duplicating the content. Here's the code, hoping someone may be of help, thanks! while(list($last_name)=mysql_fetch_array($data1)) { $data2 =...
5
by: DB_2 | last post by:
I was wondering if there was a way to create a copy of a table in a single SQL statement, together with its column structure and data. In Oracle, there is a "CREATE TABLE new-table AS...
2
by: Todd Plambeck | last post by:
I have an ASP.net order entry system that will occasionally insert duplcate orders into the database. We are using SQL Server for the session state. This also only seems to happen when the server...
2
by: Samuel R. Neff | last post by:
I'm trying to find a good way to handle Control.InvokeRequired without duplicating four lines of code in every function/event. Typically what I've seen in books is this: If InvokeRequired Then...
15
by: Christopher Benson-Manica | last post by:
Is there a general mechanism to duplicate, or provide for the duplication of, objects? As an example, suppose I need to duplicate an array. I can accomplish this with array.slice( 0 ), but that's...
8
by: Josetta | last post by:
I have found a wealth of information here on how to duplicate records in forms with subforms. I have adapted code found here to work with my forms. It works beautifully the first time I hit the...
0
by: rmli | last post by:
Duplicating a Database using RMAN http://quickdba.blogspot.com/2006/05/duplicating-database-using-rman_22.html
3
by: Darren.Ratcliffe | last post by:
Hi everyone I'm using v 2.0 of the framework with C# and am developing a web application. I am finding that my cookie names are duplicating themselves over and over again, for example when I...
2
by: Ciaran | last post by:
Hi is there a way of duplicating the same mysql_query resource result or is there a way to stop it from being cleared once it is no longer needed? I need to run a ......
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.