Fredou
Fredou

Reputation: 20120

how to correctly copy /clone a structure? and should I use a class instead?

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?

Upvotes: 2

Views: 11386

Answers (4)

ADeveloper
ADeveloper

Reputation: 21

Note for Visual Basic 2013: To copy the structure using estructure2=estructure1 Will not create a copy of estructure1 into structure2; instead it will assign a reference of structure1 to structure2. In this case accessing structure2 will give the same field values as structure1. If any of the structure1 fields change, the values accessed using structure2 will change too, because they are pointing to the same data. In this sense is not a copy at all.

Upvotes: 0

Mark Micallef
Mark Micallef

Reputation: 2797

To make a copy of a struct, you must declare a new instance of it. I usually do this by implementing my own Clone() method in the struct. For example:

Friend Structure SomeStructure
  Dim ValueA As Integer
  Dim ValueB As Integer
  Dim Values() As Integer
  Public Function Clone() As SomeStructure
    Dim theClone As SomeStructure
    theClone.ValueA = Me.ValueA
    theClone.ValueB = Me.ValueB
    ReDim theClone.Values(Me.Values.GetUpperBound(0))
    Array.Copy(Me.Values, theClone.Values, Me.Values.Length)
    Return theClone
  End Function
End Structure

Upvotes: 1

Guffa
Guffa

Reputation: 700592

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

Upvotes: 2

BC.
BC.

Reputation: 24938

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.

Upvotes: 4

Related Questions