Hi SStory,
ReDim Preserve is implemented as calls to the .NET Framework proper. This
applies to much of the VB legacy syntax. In practice, unless you're writing
'inner loop' code, the difference in speed is not outweighed, for most
developers, by the ease of developing with something familiar.
Having a C background and therefore being keen on C#, I tend to use the
Framework as much as possible. In part this allows me to universally apply any
new stuff that I learn. It's a very bendy rule, however, especially for
strings. I would probably use ReDim Preserve myself. ReDim Preserve uses
Array.Copy internally.
I gave an incorrect example. The code should have been.
Dim x(4) As byte '5 = 0..4
Dim y(3) As byte '4 = 0..3
: : :
Dim xL As Integer = x.Length
ReDim Preserve x (xL + y.Length - 1) '9 = 0..8
y.CopyTo (x, xL)
I forgot that the value in Dim and ReDim is <not> the number of items but
the upper index. [And I'm sure I'll forget some more times
]
This then leads to:
Function ArrayConcat (x() As Byte, y() As Byte) As Byte()
Dim xL As Integer = x.Length
ReDim Preserve x (xL + y.Length - 1)
y.CopyTo (x, xL)
Return x
End Function
or
Function ArrayConcat (x() As Byte, y() As Byte) As Byte()
Dim newx (x.Length + y.Length - 1) As Byte()
x.CopyTo (newx, 0)
y.CopyTo (newx, x.Length)
Return newx
End Function
Regards,
Fergus