VBA/ Declare and initialize a array of structure (1 Viewer)

Babycat

Member
Local time
Tomorrow, 05:42
Joined
Mar 31, 2020
Messages
275
Dear all.

I have structure (type) in VBA and an array

Code:
Private Type Item_property
    Itemname As String
    ItemMaterial As String
End Type

Dim ItemArray() As Item_property
And I expect to init ItemArray like:
Code:
ItemArray = { ("Knife", "Steel"); ("Ruler", "Plastic"); ("Ring", "Gold")}
but It does not work, so I have to assign one by one:
Code:
ItemArray(0).Itemname = "Knife"
ItemArray(0).ItemMaterial = "Steel"
..so on...

Is there any way to declare and init array value shortly as i expected?
 

plog

Banishment Pending
Local time
Today, 17:42
Joined
May 11, 2011
Messages
11,661
Yes, kinda, but not as a custom type. Either make it a class with a constructor which you can pass your two values to, or do away with the custom type and just make it an array of an array of strings. The array of array of strings would more similiarly follow the syntax you described you want. Less coding too.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:42
Joined
Feb 19, 2002
Messages
43,396
Looks like you should be using a table instead of an array. UNLESS this is just a class assignment.
 

Users who are viewing this thread

Top Bottom