• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

How to clone Array within a Class ? ?? ?

Please HEEEEEELLP

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:
http://www.larkware.com/Articles/CloninginVB.NET.html)


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 : L.name= "Rufus"
and I have cloned on L1, after do the shallow copy I can do: L.name="pipo", and L1.name is still with : L.name= "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)
        Next

        L1.ArrayY = ArrayTemp

But still the same reference....

Any ideas ? ? ?













0
franklinan
Asked:
franklinan
  • 2
  • 2
1 Solution
 
franklinanAuthor Commented:
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)
        Next

        L1.ArrayY = ArrayTemp

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

Bye !!


0
 
bruintjeCommented:
Hello franklinan,

you could look at

ArrayTemp = L.ArrayY.Clone

source: http://www.knowdotnet.com/articles/arrays.html

hope this helps a bit
bruintje
0
 
franklinanAuthor Commented:
Perfect ! Really elegant ....

I'll give you the points ..._I solved the problem myself but this post really deserves the points ...
0
 
bruintjeCommented:
thanks :)
 
asking questions really helps to mind jog the brain :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now