How to clone Array within a Class  ? ?? ?

Posted on 2006-04-22
Last Modified: 2010-04-23

Unbelievable :

I have a class :

Public Class ClassLine

    Private m_Name As String
    Private m_ArrayY As Double()

   Public Function ShallowCopy() As ClassLine   ' this lets to clone the details of a class, but NOT a REFERENCE !
        Return Me.MemberwiseClone
    End Function

end class

(See this to understand ShallowCopy:

Well...If I use this on the form:

Dim L1 As ClassLine = L.ShallowCopy

All is cloned ok, except the ARRAY !

That's it :

If I have : "Rufus"
and I have cloned on L1, after do the shallow copy I can do:"pipo", and is still with : "Rufus"

But is I modify the Array, the data is also modified on L1 .... WHY ? ? ?

How can I solve it  ? ?

I tried this :

L1.ArrayY = L.ArrayY
L1.Name = L.Name

and this :

 Dim ArrayTemp() As Double = L.ArrayY
        For i = 0 To UBound(L.ArrayY)
            ArrayTemp(i) = L.ArrayY(i)

        L1.ArrayY = ArrayTemp

But still the same reference....

Any ideas ? ? ?

Question by:franklinan
    LVL 1

    Author Comment

    Hey ! I achieved that :

    This works :

    Dim ArrayTemp() As Double
            ReDim ArrayTemp(UBound(L.ArrayY))
            For i = 0 To UBound(L.ArrayY)
                ArrayTemp(i) = L.ArrayY(i)

            L1.ArrayY = ArrayTemp

    But...any idea of doing this more elegant = ? ?? ??

    Bye !!

    LVL 44

    Accepted Solution

    Hello franklinan,

    you could look at

    ArrayTemp = L.ArrayY.Clone


    hope this helps a bit
    LVL 1

    Author Comment

    Perfect ! Really elegant ....

    I'll give you the points ..._I solved the problem myself but this post really deserves the points ...
    LVL 44

    Expert Comment

    thanks :)
    asking questions really helps to mind jog the brain :)

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now