|
-
Jul 20th, 2009, 01:25 AM
#1
Thread Starter
Addicted Member
ArrayList and structures
Hi all,
ArrayList is a generic structure.
Let's say I have my own structure (contained of Boolean+string+int). I want to set a dynamic array based on my own structure. How....?
Thanks!
-
Jul 20th, 2009, 01:34 AM
#2
Fanatic Member
Re: ArrayList and structures
 Originally Posted by emc2
Hi all,
ArrayList is a generic structure.
Let's say I have my own structure (contained of Boolean+string+int). I want to set a dynamic array based on my own structure. How....?
Thanks!
This is a example about dynamic arrray :
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
Dim scores() As Integer
ReDim scores(1)
scores(0) = 100
scores(1) = 200
For i = 0 To scores.Length - 1
MsgBox(scores(i))
Next
ReDim Preserve scores(2)
scores(2) = 300
For i = 0 To scores.Length - 1
MsgBox(scores(i))
Next
End Sub
End Class
If you want to keep the existing items in the Array , you can use the keyword Preserve .
ReDim Preserve scores(2) :
In this case the Array dynamically allocate one more String value and keep the existing values.
-
Jul 20th, 2009, 02:32 AM
#3
Re: ArrayList and structures
When you say "generic" don't mean it in the .NET sense, because an ArrayList is not a generic class, which is the problem. From .NET 2.0 onwards you should not use an ArrayList at all but rather use the List(Of T), which is a generic class. If you have your own type named SomeType, you would create a List(Of SomeType). The collection will then only accept SomeType objects and it will return its items as SomeType values rather than Object references.
-
Jul 20th, 2009, 04:45 AM
#4
Thread Starter
Addicted Member
Re: ArrayList and structures
Thanks. That's what I meant.
Can you write the releavnt syntax?
-
Jul 20th, 2009, 04:47 AM
#5
Thread Starter
Addicted Member
Re: ArrayList and structures
Thanks, but I woudln't prefer the "redim" statement. It's a VB6 .
 Originally Posted by manhit45
This is a example about dynamic arrray :
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
Dim scores() As Integer
ReDim scores(1)
scores(0) = 100
scores(1) = 200
For i = 0 To scores.Length - 1
MsgBox(scores(i))
Next
ReDim Preserve scores(2)
scores(2) = 300
For i = 0 To scores.Length - 1
MsgBox(scores(i))
Next
End Sub
End Class
If you want to keep the existing items in the Array , you can use the keyword Preserve .
ReDim Preserve scores(2) :
In this case the Array dynamically allocate one more String value and keep the existing values.
-
Jul 20th, 2009, 05:39 AM
#6
Re: ArrayList and structures
You dont need to do all that though, as JMC has explained you can just use the generic List(Of T) like so:
vb Code:
Dim MyCustomTypeList As New List(Of MyCustomStructure) Dim structureinstance As New MyCustomStructure structureinstance.SomeStringProperty = "a string" 'etc etc MyCustomTypeList.Add(structureinstance)
Get the idea?
EDIT: When you say "its a VB6"... do you mean your application is written in VB6? If so, you should post it in the VB6 forum and not the VB.NET forum
-
Jul 20th, 2009, 09:09 AM
#7
Re: ArrayList and structures
VB6 wouldn't have the ArrayList, so the interpretation of Redim being VB6 is that it is legacy.
My usual boring signature: Nothing
 
-
Jul 20th, 2009, 10:38 AM
#8
Re: ArrayList and structures
Ah, thats what I was hoping thanks for the clarification
-
Jul 20th, 2009, 01:39 PM
#9
Thread Starter
Addicted Member
Re: ArrayList and structures
That's exactly what I meant :-)
Thanks you all!
-
Jul 20th, 2009, 02:30 PM
#10
Re: ArrayList and structures
Dont forget to mark your thread as Resolved if you've got everything you need (go to the Thread Tools menu at the top of your first post in this thread)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|