Private Structure mystruct
Public field1 As Short
Public field2 As String
Public pairForListItem As Pair
End Structure
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim myarraylist As New ArrayList
'structures are value types (just like string and integer)
'they do not need the new keyword
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vbconcreatingyourowndatatypes.asp
Dim InstanceofMyClass As mystruct
Dim i As Integer
For i = 0 To 5
With InstanceofMyClass
.field1 = i
.field2 = "field 2 value " & i
'notice the use of the pair to hold related information
'http://msdn2.microsoft.com/en-us/library/system.web.ui.pair.aspx
.pairForListItem = New Pair("value " & i, "Text " & I)
End With
myarraylist.Add(InstanceofMyClass)
Next
Dim mylistbox As New ListBox
Dim li As ListItem
For i = 0 To myarraylist.Count - 1
'notice the use of the Ctype to cast every member within your
'arraylist to the type of your structure
InstanceofMyClass = CType(myarraylist(i), mystruct)
li = New ListItem(InstanceofMyClass.pairForListItem.First, _
InstanceofMyClass.pairForListItem.Second)
mylistbox.Items.Add(li)
Next
placeholder1.Controls.Add(mylistbox)
End Sub
--
HTH,
Phillip Williams
http://www.societopia.net http://www.webswapp.com
"mosscliffe" wrote:
Newbie struggling with VB.NET
I wish to create an arraylist of structures. I am very new to this
coding syntax, so bear with my poor syntax - please
struct mystruct
Fld1 as short
Fld2 as integer
Fld3 as string
Fld4 as short
etc
end Struct
dim myarraylist as arraylist of mystruct
How do assign values to the elements of the structures
dim next as short = 0
for example myarraylist(next).Fld1 = 45
myarraylist(next).Fld3 = "Bill"
etc
And then how would I access these values
example Dim mystring as string = myarraylist(current).Fld3
I hope you can understand what I am trying to do.
Having cracked the above - how can I populate a listbox with two of the
fields as a listitem pair.
Any sample code or pointers would be appreciated. I have looked for
answers and I have two books, but somehow this example has evaded me.
Richard