如何正确复制/克隆结构?我应该改用一个类吗? [英] how to correctly copy /clone a structure? and should I use a class instead?
问题描述
让我说一下
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
我不得不做
myCopy = CType(myStruct.Clone, myStruct())
还有另一个问题,如果使用了这种结构(例如10,000次),我应该创建一个类吗?
and another question, if that structure is used, let say, 10,000 times, should I created a class instead?
推荐答案
每个结构要查看12个字节,因此将其作为结构传递比在堆上创建一个字长的引用要便宜(其他方法)单词,使用一个类)
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)
如果您需要一次访问所有10,000,则即使它们是结构,也要在堆上创建它们的数组.
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.
这篇关于如何正确复制/克隆结构?我应该改用一个类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!