ArrayList and Structures VB.NET

Discussion in 'ASP .Net' started by mosscliffe, Apr 13, 2006.

  1. mosscliffe

    mosscliffe Guest

    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
    mosscliffe, Apr 13, 2006
    #1
    1. Advertising

  2. 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
    >
    >
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Apr 13, 2006
    #2
    1. Advertising

  3. mosscliffe

    mosscliffe Guest

    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)
    mosscliffe, Apr 13, 2006
    #3
  4. You are very welcome, Richard.
    --
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "mosscliffe" wrote:

    > 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)
    >
    >
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Apr 14, 2006
    #4
  5. mosscliffe

    ekennedy

    Joined:
    Sep 22, 2008
    Messages:
    1
    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?
    ekennedy, Sep 22, 2008
    #5
  6. mosscliffe

    bruce2424

    Joined:
    Jun 27, 2008
    Messages:
    2
    bruce2424, Oct 23, 2008
    #6
  7. mosscliffe

    Timwaterbury1

    Joined:
    Nov 14, 2011
    Messages:
    3
    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
    Timwaterbury1, Nov 14, 2011
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,723
    Natty Gur
    Aug 19, 2003
  2. Kaidi
    Replies:
    4
    Views:
    2,318
    Kaidi
    Jan 3, 2004
  3. Alfonso Morra
    Replies:
    11
    Views:
    696
    Emmanuel Delahaye
    Sep 24, 2005
  4. xz
    Replies:
    16
    Views:
    2,337
  5. Philipp
    Replies:
    6
    Views:
    891
    Arne Vajhøj
    May 28, 2008
Loading...

Share This Page