Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Hi,

I have 2 arrays, I don't know their length...

I wanna copy both arrays into 1 single array.

So the length of output array will be sum of my 2 input arrays.

I know I should use Array.Copy but don't know how, for my case, it won't merge the contents of 2 arrays into 1.

Anyone can help me?

 

I also used this but won't work:

ReDim OutputArray(InputArray1.Length + InputArray2.Length - 1)
Array.Copy(InputArray1, OutputArray, InputArray1.Length)
Array.Copy(InputArray2, OutputArray, InputArray2.Length)

 

Thanks :)

Edited by VBOfficer
  • Leaders
Posted

You need to specify where to begin copying to when calling Array.Copy using a different overload (at least with InputArray2, since you don't want to copy that to the beginning of OutputArray).

ReDim OutputArray(InputArray1.Length + InputArray2.Length - 1)
       ' Parameters are sourceArray, sourceOffset, destArray, destOffset, length)
       Array.Copy(InputArray1, 0, OutputArray, 0, InputArray1.Length)
       Array.Copy(InputArray2, 0, OutputArray, InputArray1.Length, InputArray2.Length)

In the last line, the code specifies to copy InputArray2 starting where InputArray1 left off (the second to last parameter).

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...