Duplicating arrays

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

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
 
Dave said:
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).
 
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
 
Dave said:
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))
///
 
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
message
 
Sorry,

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

Dave
 
Is your third line correct? The one that reads:

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

Original.CopyTo(Copy,0)

?
 
Chris said:
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...
 

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

Back
Top