How to initialize a struct in the declaration statement?

  • Thread starter Thread starter Bill Nicholson
  • Start date Start date
B

Bill Nicholson

I declared a UDT...

Structure TeamRankingsStruc

Public intRankings() As Int16 ' The dimension must be omitted in the
context of a data type definition

Public strName As String

End Structure

I want to initialize it in the declaration statement...

Dim objRankings( ) As TeamRankingsStruc = {???}

What's the syntax? I've tried every combination of braces and parens that I
can think of.

Thanks!



Bill Nicholson

Cincinnati, OH USA
 
Bill Nicholson said:
I declared a UDT...

Structure TeamRankingsStruc

Public intRankings() As Int16 ' The dimension must be omitted in
the
context of a data type definition

Public strName As String

End Structure

I want to initialize it in the declaration statement...

Dim objRankings( ) As TeamRankingsStruc = {???}

What's the syntax? I've tried every combination of braces and parens
that I can think of.


Dim objRankings( ) As TeamRankingsStruc = {new TeamRankingsStruc, new
TeamRankingsStruc}

You could alo add a constructor to the structure:

Public Sub New(ByVal UBound As Integer)
ReDim intRankings(UBound)
End Sub

Dim objRankings( ) As TeamRankingsStruc = _
{new TeamRankingsStruc(5), new TeamRankingsStruc(10)}


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Thank You, but this does not initialize the individual data structures in
the array, instead it establishes a size for the array of structures.

I want to do something like this...

Dim objRankings( ) As TeamRankingsStruc = {(1, "Indiana"), (2, "Ohio
State"), (3, "Purdue")}

Regards,

Bill
 
* "Bill Nicholson said:
Thank You, but this does not initialize the individual data structures in
the array, instead it establishes a size for the array of structures.

I want to do something like this...

Dim objRankings( ) As TeamRankingsStruc = {(1, "Indiana"), (2, "Ohio
State"), (3, "Purdue")}

\\\
Dim atrs() As TeamRankingsStruc = {New TeamRankingsStruc(1, "NY"), New TeamRankingsStruc(2, "Foo")}
..
..
..
Public Structure TeamRankingsStruc
Public Sub New(ByVal Rank As Integer, ByVal Name As String)
Me.Rank = Rank
Me.Name = Name
End Sub

Public Rank As Integer
Public Name As String
End Structure
///
 
Bill Nicholson said:
Thank You, but this does not initialize the individual data
structures in the array, instead it establishes a size for the array
of structures.

I want to do something like this...

Dim objRankings( ) As TeamRankingsStruc = {(1, "Indiana"), (2,
"Ohio State"), (3, "Purdue")}


Add another parameter to the constructor:

Public Sub New(ByVal UBound As Integer, Name as string)
ReDim intRankings(UBound)
Me.strName = Name
End Sub

Dim objRankings( ) As TeamRankingsStruc = _
{New TeamRankingsStruc (1, "Indiana"), _
New TeamRankingsStruc (2, "Ohio State"), _
New TeamRankingsStruc (3, "Purdue")}


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Back
Top