Question How do I initialize an array of User Defined Structure?

asegal0000

Member
Joined
May 12, 2009
Messages
5
Programming Experience
10+
Structure AWGInfo
Dim AWG As Integer
Dim DiamMM As Double
Dim InsulDiamMM As Double
Dim GramsPerMeter As Double
Dim OhmsPerMeter As Double
End Structure
Dim X() As AWGInfo = {{1, 2.1, 3.1, 4.1, 5.1}, {6, 7.1, 8.1, 9.1, 10.2}}

On the Dim X() line, I get this error:
BC30332 Value of type 'Double(*,*)' cannot be converted to 'Form1.AWGInfo()' because 'Double' is not derived from 'Form1.AWGInfo'.
[Column 23]

If I change it to Dim X() As AWGInfo = {{1, 2.1, 3.1, 4.1, 5.1}, {6, 7.1, 8.1, 9.1, 10.2}}
I also have the error:
BC30672 Explicit initialization is not permitted for arrays declared with explicit bounds.

What I would like is:
X(0)
.AWG =1
.DiamMM = 2.1
.InsulDiamMM = 3.1
.GramsPerMeter = 4.1
.OhmsPerMeter = 5.1
X(1)
.AWG = 6
.DiamMM = 7.1
.InsulDiamMM = 8.1
.GramsPerMeter = 9.1
.OhmsPerMeter = 10.2

I know I can fill the array with code, but I prefer to do it at compile time just because of the amount of data that I have.

Any suggestions?
 
Your post is so convenient that it begs for a With block, school assignment? :watermelon:
        Dim X(1) As AWGInfo
        With X(0)
            .AWG = 1
            .DiamMM = 2.1
            .InsulDiamMM = 3.1
            .GramsPerMeter = 4.1
            .OhmsPerMeter = 5.1
        End With
        With X(1)
            'etc
        End With

Then there is the With object initializer, much alike the above, typically used like this:
X(0) = New AWGInfo With {.AWG = 1, .DiamMM = 3.1}


You can also add a constructor (Sub New) to the structure and use that - a.k.a New AWGInfo(1, 2.1, 3.1, 4.1, 5.1)
 
No, not school.
I am working on figuring out how many turns of wire I need on a coil for a 1950 jukebox.

Is there way to do the "with" for multiple items: - that is fill x(0)..x(40) in a single statement.
For some reason I dont like the:
X(0) = New AWGInfo With {.AWG = 1, .DiamMM = 3.1}
X(1) = New AWGInfo With {.AWG = 20, .DiamMM = .3}
with x(0), with x(1), although I will use that if nothing cleaner looks cleaner;
 
Is there way to do the "with" for multiple items: - that is fill x(0)..x(40) in a single statement.
Yes, a For loop... For index, With array(index).
 
Back
Top