Before I attempt to answer this question, I would suggest that you
always specify what VB.NET version your using... Answers to questions
can vary based on that information. For example, in your case if your
using VB.NET 2005, then I would tell you not to use ArrayList at all -
but to use System.Collections.Generic.List.
I have a simple structure for example purposes I add to an array list
public structure MyStruct
dim counter as integer
end structure
dim mylist as ArrayList
dim x as New MyStruct
x.counter = 2
mylist.Add(x)
debug.writeline (mylist(0).counter) shows a value of 2
I try to change the value
mylist(0).counter = 3
and I get an error:
**Latebound assignment to a field of value type 'MyStruct'is not valid when
'MyStruct' is the result of a latebound expression.
That's because you don't have option strict on. You should pretty much
always have that on (there are interop situations where it is nice to
turn it off - but, for most code, you're asking for issues by not
turning it on).
VB.NET 1.1
Option Explicit On
Option Strict On
Imports System
Imports System.Collections
Module Module1
Structure MyStruct
Public counter As Integer
Public Sub New(ByVal initialValue As Integer)
counter = initialValue
End Sub
End Structure
Sub Main()
Dim myList As New ArrayList
Dim x As New MyStruct(2)
' set the initial value
myList.Add(x)
' change the element
Dim y As MyStruct = DirectCast(myList(0), MyStruct)
y.counter = 3
myList(0) = y
' get it back to show it was changed
Dim z As MyStruct = DirectCast(myList(0), MyStruct)
Console.WriteLine(z.counter)
End Sub
End Module
VB.NET 2005:
Option Explicit On
Option Strict On
Imports System
Imports System.Collections.Generic
Module Module1
Structure MyStruct
Public counter As Integer
Public Sub New(ByVal initialValue As Integer)
counter = initialValue
End Sub
End Structure
Sub Main()
Dim x As New MyStruct(2)
Dim myList As New List(Of MyStruct)
myList.Add(x)
Dim y As MyStruct = myList(0)
y.counter = 3
myList(0) = y
Dim z As MyStruct = myList(0)
Console.WriteLine(z.counter)
End Sub
End Module
All of this becomes a lot easier if you just make this a reference type
( a class rather then a value type)...
vb.net 1.1
Option Explicit On
Option Strict On
Imports System
Imports System.Collections
Module Module1
Class MyStruct
Public counter As Integer
Public Sub New(ByVal initialValue As Integer)
counter = initialValue
End Sub
End Class
Sub Main()
Dim myList As New ArrayList
Dim x As New MyStruct(2)
' set the initial value
myList.Add(x)
' change the element
DirectCast(myList(0), MyStruct).counter = 3
' get it back to show it was changed
Console.WriteLine(DirectCast(myList(0), MyStruct).counter)
End Sub
End Module
vb.net 2005:
Option Explicit On
Option Strict On
Imports System
Imports System.Collections.Generic
Module Module1
Class MyStruct
Public counter As Integer
Public Sub New(ByVal initialValue As Integer)
counter = initialValue
End Sub
End Class
Sub Main()
Dim x As New MyStruct(2)
Dim myList As New List(Of MyStruct)
myList.Add(x)
myList(0).counter = 3
Console.WriteLine(myList(0).counter)
End Sub
End Module
HTH