ArrayList and Structures VB.NET

M

mosscliffe

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
 
G

Guest

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/d.../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
 
M

mosscliffe

Mr Williams,

I am speechless. I go and have something to eat and when I return,
there is the perfect answer, including the listitem bit. I am still a
bit flaky about your answer, but I will copy it to my routine and with
a few response.writes added, I will try to understand how it all works.

I have always found the usenet community very helpful and it is nice to
know it is still going strong, despite all the forums and blogs that
are available today. I was able to help and receive help in my shell
scripting days in Unix and that dirty word Xenix !!

Thanks again

Richard (Manchester, UK)
 
Joined
Sep 22, 2008
Messages
1
Reaction score
0
When using an arraylist with a structure or class like the earlier example, are you able to sort the arraylist based on one of the fields in the structure?
 
Joined
Nov 14, 2011
Messages
3
Reaction score
0
IP array list for Class problems!!!

I am having trouble with my vb script for my class. I have everything, it just will not show the cscript or cmd prompt when I run it.

Here is the Script, can anyone help?


' VBScript: IP_Array_start.vbs
' Written by: Tim Waterbury
' Date: 11/13/2011
' Class: COMP230
' Professor: Arias, Ruben
' ===================================
' This initialize a 2-dimension array
' of IP Address. The first index +100
' is the room# and the second index+1
' is the computer# in the room.
dim ipAddress(5,3)
ipAddress(0,0)="192.168.10.11"
ipAddress(0,1)="192.168.10.12"
ipAddress(0,2)="192.168.10.13"
ipAddress(0,3)="192.168.10.14"
ipAddress(1,0)="192.168.10.19"
ipAddress(1,1)="192.168.10.20"
ipAddress(1,2)="192.168.10.21"
ipAddress(1,3)="192.168.10.22"
ipAddress(2,0)="192.168.10.27"
ipAddress(2,1)="192.168.10.28"
ipAddress(2,2)="192.168.10.29"
ipAddress(2,3)="192.168.10.30"
ipAddress(3,0)="192.168.10.35"
ipAddress(3,1)="192.168.10.36"
ipAddress(3,2)="192.168.10.37"
ipAddress(3,3)="192.168.10.38"
ipAddress(4,0)="192.168.10.43"
ipAddress(4,1)="192.168.10.44"
ipAddress(4,2)="192.168.10.45"
ipAddress(4,3)="192.168.10.46"
ipAddress(5,0)="192.168.10.51"
ipAddress(5,1)="192.168.10.52"
ipAddress(5,2)="192.168.10.53"
ipAddress(5,3)="192.168.10.54"

' Define program vaiables
roomStr=""
compStr=""
room=0
computer=0

Do
Display Prompt “Please Enter the Room Number (100-105) ...... “
Get StdIn Console Input and assign string value to roomStr
If intval(roomStr)<100 Or intval(roomStr)>105
Use StdOut to Beep Speaker twice with chr(7)
Display ErrMsg “Error, 100 to 105 Only!!!”
else
room = inval(roomStr)-100
endif
While intval(roomStr)<100 OR intval(roomStr)>105


Do
Display Prompt “Please Enter the Computer Number (1-4) ...... “
Get StdIn Console Input and assign string value to compStr
If intval(compStr)<1 Or intval(compStr)>4
Use StdOut to Beep Speaker twice with chr(7)
Display ErrMsg “Error, 1 to 4 Only!!!”
else
room = inval(compStr)-1
endif
While intval(compStr)<1 OR intval(compStr)>105

Display the message “ The IP Address in Room ### for computer # is ###.###.##.##:

' Display All IP Address Y/N?
Do
Display Message
"Do you wish to Display all of the IP Addresses (Y/N) ..... "
Get User Response and assign it to variable ans
If ans NOT "Y" & "y" & "N" & "n" Then
Beep Speaker Twice
Display Msg "Error, Y,y,N,n response Only!!!"
EndIf
While ans NOT "Y" & "y" & "N" & "n"

If ans is "Y" OR ans is "y" Then
For room = 0 to 5
For computer = 0 to 3
Display "The IP Address in Room " toString(room+100) “for Computer "
toString(computer+1) " is " ipAddress(room,computer)
EndFor
EndFor
EndIf
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top