2

I have a structure A that contain 2 integer, 1 String, and 1 array of another Struct B.

In a function, i want to initialize the size of the array of structure B, but Microsoft Visual Basic don't want to accept all try I do.

the struct A

    Structure XpGrpData
        Dim Mode As Integer
        Dim XpValue As Integer
        Dim Name As String
        Dim player As XpUsrData()
    End Structure

the struct B

    Structure XpUsrData
        Dim Mode As Integer
        Dim XpValue As Integer
        Dim Name As String
    End Structure

how i do normally the thing

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

But my IDE says "Too Much Arguments for 'Public Sub New()'". How I can set the size of my array ?

ghost
  • 23
  • 2
  • 6
  • 3
    You need to add a constructor `Sub New()` that takes an argument for every field in that struct. Also.... ideally a struct is *immutable* - consider making it a `Class` if you're treating it as any old object. – Mathieu Guindon May 01 '19 at 19:20
  • I agree with Mr. Guindon. Also use `List(Of YourClass)` instead of an array, It's much better and you can access through your items more efficiently. – IOviSpot May 02 '19 at 20:49

2 Answers2

0

As you have it right now, to instantiate the structure I would use:

Dim CurrentXpData as XpGrpData = New XpGrpData()

When passing an argument into the constructor it is expecting you to have defined what you want to do with argument. For example in a struct / class you would add a New() subroutine:

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

As already mentioned you may want to consider a Class unless you specifically need a Struct. They handle memory differently and a class is usually advantageous.

Jacob M.
  • 750
  • 1
  • 6
  • 21
0

This pattern worked for me to work around the awkward syntax error:

Dim temp(myValue) As XpGrpData
CurrentXpData.player = temp
Govert
  • 16,387
  • 4
  • 60
  • 70