In VB6 you could get away with the following code: Dim Index As Integer Dim ItemsCount As Integer Dim StringArray() As String Dim StringValue As String '.... 'ItemsCount is determined from a database query, or a user-entry, etc. '.... For Index = 0 To ItemsCount
- 1 'StringValue is determined from a database query, or a user-entry, etc. ReDim Preserve StringArray(0 To Index) As String StringArray(Index) = StringValue Next Index However, you can't do this in VB.NET ... (compile-time error on ReDim) What is an alternative
coding method ? Thanks.
Why don't you use a Collection object such as ArrayList or Hashtable? Or even create your own strictly typed collection by deriving from CollectionBase. I personally try to stay away from straight arrays where I can. I usually feel the overhead of a collection
is minimal compared to it's additional capabilities.
I rarely log in at work, but the development machine is bogged down and this post urks me... Why would you use ReDim when you know the exact size ahead of time? ReDim, mixed with Preserve can be very exhaustive in this very unnecessary case (though there are
times where it proves necessary).
Dim ItemsCount As Integer
Dim StringArray() As String = Nothing
Dim Index As Integer
' ItemsCount gotten from DB
ItemsCount = 123
' you could remove the ItemsCount - 1 below by adding a line to take it away ItemsCount -= 1
StringArray = New String(ItemsCount - 1) { } ' the {} is key for the compiler
For Index = 0 To ItemsCount - 1
StringArray(Index) = value
Next
If you are not guaranteed that the string array will need that much space, then use a collection (System.Collections). Using ReDim would be a big waste of resources. The forums are NOT posting this message... this is very
annoying.
johnagrandy
Participant
1670 Points
362 Posts
alternative to ReDim trick possible in VB6
Nov 10, 2004 02:45 AM|LINK
rsmoke21
Contributor
3931 Points
792 Posts
Re: alternative to ReDim trick possible in VB6
Nov 10, 2004 12:38 PM|LINK
pickyh3d
Star
9696 Points
1955 Posts
Re: alternative to ReDim trick possible in VB6
Nov 11, 2004 12:54 PM|LINK
Dim ItemsCount As Integer Dim StringArray() As String = Nothing Dim Index As Integer ' ItemsCount gotten from DB ItemsCount = 123 ' you could remove the ItemsCount - 1 below by adding a line to take it away ItemsCount -= 1 StringArray = New String(ItemsCount - 1) { } ' the {} is key for the compiler For Index = 0 To ItemsCount - 1 StringArray(Index) = value NextIf you are not guaranteed that the string array will need that much space, then use a collection (System.Collections). Using ReDim would be a big waste of resources. The forums are NOT posting this message... this is very annoying.