473,394 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

ArrayList and Structures VB.NET

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

Apr 13 '06 #1
3 5460
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

Apr 13 '06 #2
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)

Apr 13 '06 #3
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)

Apr 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rustam Asgarov | last post by:
Hi. I have ArrayList that contains structures... I need to iterate through all structures in this array and update one of members in this structures... The first thing that comes to mind is just...
1
by: RBCC | last post by:
I have the following structure: Structure member Dim name As String Dim phone As String End Structure Dim memberarray As New ArrayList Dim membership As member ******************** Ihave...
3
by: Dennis | last post by:
I am trying to bind a DataGrid Control (named DataGrid1) to an array list containing elements of a sturcture type (named TestDataGrid). From what I've read, it appears possible but the following...
8
by: Maileen | last post by:
Hi, I would like to have something like an array (something like a collection) in order to store the following things : company name1, new1, old1, in1, out1 company name2, new2, old2, in2,...
1
by: Joe | last post by:
Hello All: I am writing a function which accepts an ArrayList as its parameter and converts the contents of the ArrayList into an XmlNodeList. The ArrayList will contain one of several...
18
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point...
3
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: ...
1
by: =?Utf-8?B?SmF5Qw==?= | last post by:
I am trying to understand how to use an arraylist that contains data in a structure and bind the results to a gridview. Using vs2008 I have looked at the examples 315784 HOW TO: Bind a DataGrid...
12
by: Justin | last post by:
Ok, I give up. I can't seem to construct a decent (productive) way of sorting my arraylist. I have a structure of two elements: Structure TabStructure Dim TabName As String Dim FullFilePath...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.