【问题标题】:how i can declare a array of structure in a structure in VB?如何在 VB 的结构中声明结构数组?
【发布时间】:2019-05-01 19:12:21
【问题描述】:

我有一个结构 A,其中包含 2 个整数、1 个字符串和另一个结构 B 的 1 个数组。

在一个函数中,我想初始化结构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()' 的参数太多”。 如何设置数组的大小?

【问题讨论】:

  • 您需要添加一个构造函数Sub New(),该构造函数为该结构中的每个字段提供一个参数。另外....理想情况下,结构是不可变的 - 如果您将其视为任何旧对象,请考虑将其设为Class
  • 我同意金登先生的观点。也可以使用List(Of YourClass)而不是数组,这样会更好,您可以更有效地访问您的项目。

标签: vb.net structure


【解决方案1】:

正如你现在所拥有的,实例化我将使用的结构:

Dim CurrentXpData as XpGrpData = New XpGrpData()

当将参数传递给构造函数时,它期望您已经定义了要对参数执行的操作。例如,在结构/类中,您将添加一个 New() 子例程:

public sub New(myInput)
    me.someProperty = myInput
end sub

如前所述,您可能需要考虑使用 Class,除非您特别需要 Struct。 They handle memory differently and a class is usually advantageous

【讨论】:

    【解决方案2】:

    这个模式帮我解决了尴尬的语法错误:

    Dim temp(myValue) As XpGrpData
    CurrentXpData.player = temp
    

    【讨论】:

      猜你喜欢
      • 2021-03-23
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多