Here I have:
Public Structure MyStruct Public Name as String Public Content as String End Structure Dim oStruct as MyStruct = New MyStruct() oStruct.Name = ... oStruct.Content = ... Dim alList as ArrayList = new ArrayList() alList.Add(oStruct)
I’d like to convert the ArrayList to a static strongly-typed Array of type MyStruct. How can I do that? I had no luck with ToArray.
I am using .NET Framework 2.0.
I assume that since you are using ArrayList, you are using 1.1?
In which case, I suspect the following would work:
(edit – Bill’s ToArray usage is more convenient – I didn’t know about that one, but then, I very rarely [if ever] use ArrayList)
However, if MyStruct really is a struct, then I cannot say strongly enough that mutable structs are a bad idea – i.e. where you can set .Name and .Content after creation. Structs should almost always be immutable. In reality, your MyStruct looks like it should be a class. Also – I’m not ‘up’ on VB, but are they public fields? Again – not recommended – properties would be preferable. I don’t know about VB, but C# 3.0 has some very terse syntax for this:
If you are using 2.0 or above, consider List<T> instead of ArrayList.