Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Appending arrays - HOWTO? 1

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
571
US
Colleagues,
Can't recall if I've asked this Q. B 4, forgive me if I had. [bowright]

Say, you have two arrays of the same data type, and you need to add/concatenate/append them into one.
Besides the obvious (ReDim Preserve one array with the sum of elements and copy the elements of the second one to the empty sells of the first one) -
is there a function in the .NET that does exactly that? Something like (tried, dinna work)

Code:
Dim la1() As String = {"1", "2", "3"}
Dim la2() As String = {"4", "5", "6"}

Dim laS() As String = la1 & la2 ' Erred, "[COLOR=#CC0000]Error	BC30452	Operator '&' is not defined for types 'String()' and 'String()'[/color]"

(I ran the search in MS .NET Help, found nothing helpful...)

Regards,

Ilya
 
Have you tried something like this?

Code:
Dim la1() As String = {"1", "2", "3"}
Dim la2() As String = {"4", "5", "6"}
Dim laS() As String = la1.Concat(la2).ToArray()

or

Code:
Dim la1() As String = {"1", "2", "3"}
Dim la2() As String = {"4", "5", "6"}
Dim laS(la1.Length + la2.Length - 1) As String
la1.CopyTo(laS, 0)
la2.CopyTo(laS, la1.Length)

or

Code:
Dim la1() As String = {"1", "2", "3"}
Dim la2() As String = {"4", "5", "6"}
Dim laS(la1.Length + la2.Length - 1) As String
Array.Copy(la1, laS, la1.Length)
Array.Copy(la2, 0, laS, la1.Length, la2.Length)
 
I like this Concat() the best! [thanks2] , StrongM!

Actually, I need to concatenate 3 arrays, so I tried (C/C++ style, y'kno)

Code:
Dim la1() As String = {"1", "2", "3"}
Dim la2() As String = {"4", "5", "6"}
Dim la3() As String = {"7", "8", "9"}
Dim laS() As String = la3.Concat(la1.Concat(la2).ToArray()).ToArray()

and it worked!

Regards,

Ilya
 
Doesn't mikrom deserve some credit [ponder]

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Forgot to click on star - mea culpa!
Done now.

Regards,

Ilya
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top