tags:

views:

46

answers:

2

let say i have that

Structure myStruct
    Public myPoint As Point
    Public myBool As Boolean
End Structure

how to I make a copy / clone of that structure?

I fixed that issue now, example of the code I was using:

    Dim myStruct(1) As myStruct 
    myStruct(0).myPoint = New Point(10, 10)
    myStruct(0).myBool = True

    Dim myCopy(1) As myStruct
    myCopy = myStruct
    myCopy(0).myBool = False
    myCopy(0).myPoint = New Point(11, 11)

with that, both variable was changed

I had to do

    myCopy = CType(myStruct.Clone, myStruct())

and another question, if that structure is used, let say, 10,000 times, should I created a class instead?

+2  A: 

You're looking at 12 bytes per structure, so passing it around as a struct is cheaper than creating a word-sized reference to it on heap (in other words, using a class)

If you need to access all 10,000 at once, creating an array of them will happen on the heap even if they are structs.

Copying the struct is as easy as creating declaring another struct of the same type and assigning the first to the second.

BC
do you mean something structVar1 == structVar2 ? that doesn't make a copy
Fredou
@Fredou No, structVar1 = structVar2.
BC
or maybe it does, I just retried it and it seem to work...
Fredou
@Fredou, a == b is a compare. a = b is an assignment. (At least in C#.)
strager
@strager, I remembered that when I clicked Add comment, was too late to change it
Fredou
without an array, var1 = var2 work well, not with an array, see my question for how i fixed it...
Fredou
thanks, I just switched a class into a structure, removing a lot of code.
Fredou
A: 

The reason that you had to use the Clone method was that you were copying the Array object, not the structure. This works for copying the structure value:

Dim myStructArray(0) As MyStruct
myStructArray(0).MyPoint = New Point(10, 10)
myStructArray(0).MyBool = True

Dim myCopyArray(0) As MyStruct
myCopyArray(0) = myStructArray(0) 'copy structure instead of array
myCopyArray(0).MyBool = False
myCopyArray(0).MyPoint = New Point(11, 11)

In this case it's of course totally pointless to copy the structure as you are replacing all the values, but the copying does work, leaving you with two separate arrays instead of two references to the same array.

A structure should generally be immutable, i.e. it's properties are read-only, which protects you from situations like this:

Dim myStructList(Of MyStruct) As New myStructList(Of MyStruct)()
myStructList.Add(New MyStruct())
myStructList(0).MyPoint = New Point(10, 10) 'doesn't work

The reason that this doesn't work is that myStructList(0) returns a copy of the structure. The MyPoint member of the copy is changed, but the copy of the structure is never written back to the list.

This is the immutable structure:

Structure MyStruct

   Private _myPoint As Point
   Private _myBool As Boolean

   Public Readonly Property MyPoint As Point
      Get
         Return _myPoint
      End Get
   End Property

   Public Readonly Property MyBool As Boolean
      Get
         Return _myBoolean
      End Get
   End Property

   Public Sub New(ByVal myPoint As Point, ByVal myBool As Boolean)
      _myPoint = myPoint
      _myBool = myBool
   End Sub

End Structure

You create a new value using the constructor:

Dim myStructList(Of MyStruct) As New myStructList(Of MyStruct)()
myStructList.Add(New MyStruct(New Point(10, 10), True))

You can still copy the whole structure value:

Dim myStructArray(0) As MyStruct
myStructArray(0) = New MyStruct(New Point(10, 10), True)

Dim myCopyArray(0) As MyStruct
myCopyArray(0) = myStructArray(0) 'copy structure value
Guffa