How to Initialise a Structure Array in a Dim Statement

  • Thread starter Thread starter Charles Law
  • Start date Start date
C

Charles Law

Does anyone know if it is possible to initialise a structure array at
run-time, something like this:

<code>
Structure struct
Dim a As String
Dim b As Integer
Dim c As [Delegate]
End Structure

....

Dim st() As struct = {"1", 2, AddressOf Foo1}, _
{"2", 2, AddressOf Foo2}
</code>

I realise that this syntax is incorrect, but is there a syntax for this type
of initialisation?

TIA

Charles
 
Here's one way - you could provide a constructor to initialize the values of
the structure. Something like this:

Structure struct
Dim a As String
Dim b As Integer
Dim c As [Delegate]
Public Sub New(ByVal a As String, ByVal b As Integer, ByVal c As
[Delegate])
a = a
b = b
c = c
End Sub
End Structure

Then initialize the structures as:

Dim st() As Struct = {New Struct("1", 2, AddressOf foo1), _
New Struct("2", 2, AddressOf foo2)}


hope that helps..
Imran.
 
Hi Imran

Funnily enough, I have just been turning it over in my mind, and had
wondered if that might work. Thanks for confirming it. I suppose it is quite
elegant too, but I wonder if there is a way to do it without the New
keyword.

Charles


Imran Koradia said:
Here's one way - you could provide a constructor to initialize the values
of
the structure. Something like this:

Structure struct
Dim a As String
Dim b As Integer
Dim c As [Delegate]
Public Sub New(ByVal a As String, ByVal b As Integer, ByVal c As
[Delegate])
a = a
b = b
c = c
End Sub
End Structure

Then initialize the structures as:

Dim st() As Struct = {New Struct("1", 2, AddressOf foo1), _
New Struct("2", 2, AddressOf foo2)}


hope that helps..
Imran.

Charles Law said:
Does anyone know if it is possible to initialise a structure array at
run-time, something like this:

<code>
Structure struct
Dim a As String
Dim b As Integer
Dim c As [Delegate]
End Structure

...

Dim st() As struct = {"1", 2, AddressOf Foo1}, _
{"2", 2, AddressOf Foo2}
</code>

I realise that this syntax is incorrect, but is there a syntax for this type
of initialisation?

TIA

Charles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top