我有一个结构A,包含2个整数,1个字符串和1个另一个结构B的数组。
在一个函数中,我想初始化结构B的数组的大小,但Microsoft Visual Basic不想接受我做的所有尝试。
结构A
Structure XpGrpData
Dim Mode As Integer
Dim XpValue As Integer
Dim Name As String
Dim player As XpUsrData()
End Structure
结构B
Structure XpUsrData
Dim Mode As Integer
Dim XpValue As Integer
Dim Name As String
End Structure
我是怎么做的
Dim CurrentXpData As XpGrpData
CurrentXpData.player = New XpGrpData(myValue)
但是我的IDE说“'Public Sub New()'”太多了。我如何设置数组的大小?
正如您现在所拥有的那样,要实例化我将使用的结构:
Dim CurrentXpData as XpGrpData = New XpGrpData()
将参数传递给构造函数时,它期望您已经定义了要对参数执行的操作。例如,在struct / class中,您将添加一个New()子例程:
public sub New(myInput)
me.someProperty = myInput
end sub
如前所述,除非您特别需要Struct,否则您可能需要考虑Class。 They handle memory differently and a class is usually advantageous。