Results 1 to 6 of 6

Thread: [RESOLVED] [2005] Copy Array(of Class) to another

  1. #1

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Resolved [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?
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  2. #2
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    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.

  3. #3
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    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

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,990

    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

  5. #5

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    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.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  6. #6
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    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
  •  



Click Here to Expand Forum to Full Width