-
Mar 12th, 2009, 07:52 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] [2005] Copy Array(of Class) to another
I am trying to copy an Array(of Class) to another Array(of Class). The Class really just holds 3 values together, so it's basically a string. I thought just something like this would work
Code:
Dim Array1 as Array(of Class) = Array2
'also tried
Dim Array1 as New Array(of Class)
Array1 = Array2
But that literally makes Array1 access the contents of 2, but I want it to be a separate copy. I'm currently using a simple loop procedure to copy contents but I was hoping there was a command for it. I looked at .Item but there didn't seem to be anything there. Anyone have a solution?
-
Mar 12th, 2009, 08:19 PM
#2
Re: [2005] Copy Array(of Class) to another
Doesnt this work in your situation?
Array.Copy(Array1,Array2)
- If my post helped you, please Rate it
- If your problem is solved please also mark the thread resolved
I use VS2015 (unless otherwise stated).
_________________________________________________________________________________
B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.
-
Mar 12th, 2009, 08:38 PM
#3
Re: [2005] Copy Array(of Class) to another
Bulldog, I don't think that Array.Copy does a deep copy of the objects, so for classes (which are reference objects) only the reference will get copied.
I think what would need to be done would be to implement a clone method on the object, something like this:
Code:
Friend Class Blah
Implements ICloneable
Public s1 As String
Public s2 As String
Public s3 As String
Public Function Clone() As Object Implements System.ICloneable.Clone
Return Me.MemberwiseClone
End Function
End Class
-
Mar 12th, 2009, 08:43 PM
#4
Re: [2005] Copy Array(of Class) to another
Yeah, if they were structures, you could copy them with Array.Copy, but since they are reference types there is no shortcut. The reason for this has to do with the fact that there is no 'correct' way to perform a deep copy of reference types, so no language adds that feature. To understand that, consider what would happen if you had an array of classes that wrapped database connections (why you'd do this is beyond me, but it makes a good example). To perform a copy, should new connections be created? Should they be opened? Or should the existing connection be copied? Since different results should happen in different situations, the compiler can't know what to do. Therefore, a shallow copy is all you get. To do more you have to write it yourself.
My usual boring signature: Nothing
-
Mar 12th, 2009, 09:16 PM
#5
Thread Starter
Fanatic Member
Re: [2005] Copy Array(of Class) to another
So I guess it can't be done then. Thanks for confirming it though, and to anyone who finds this topic looking for a solution, here's the code I used:
Code:
Public Sub CopyList(ByVal Source As List(Of VocabList), ByVal Destination As List(Of VocabList))
Destination.Clear()
For x As Integer = 0 To Source.Count - 1
Destination.Add(New VocabList(Source(x).Word, Source(x).PartOfSpeech, Source(x).Definition))
Next
End Sub
You would of course replace VocabList with the name of your class and replace New...etc. with your class New Function and its parameters.
-
Mar 13th, 2009, 09:56 AM
#6
Re: [RESOLVED] [2005] Copy Array(of Class) to another
Typically, you's implement a 'clone' method if an object is to be copied. In the Clone method you'd put code in to perform the copy appropriate to the object (as Shaggy stated) and return a new object - a copy of the original one.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|