Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Does the .Net framework have any built-in support for joining arrays?

 

I couldn't find anything useful, and the things I tried didn't work. Is the only way to do it by adding each individual element?

 

byte[] bFirstArray = new byte[6] { 1, 2, 3, 4, 5, 6 };
byte[] bSecondArray = new byte[6] { 7, 8, 9, 10, 11, 12 };
byte[] bThirdArray = null;

//bThirdArray = (byte[])(bFirstArray.ToString() +  bSecondArray.ToString());  //Doesn't work.
//bThirdArray = new byte[] { bFirstArray, bSecondArray }; //Doesn't work.

  • Leaders
Posted

As far as I know, your best bet is to manually combine the arrays...

 byte[] Array1 = { 1, 2, 3, 4, 5, 6 };
 byte[] Array2 = { 7, 8, 9, 10, 11, 12 };
 byte[] Array3 = new byte[12];
 Array1.CopyTo(Array3, 0);
 Array2.CopyTo(Array3, Array1.Length);

 

Unless you want to create your own function to do it for you...

// Haven't tested this function
static System.Array JoinArrays(System.Array Array1, System.Array Array2) {
   // Throw exception if array types don't match
   if(! Array1.GetType().Equals(Array2.GetType())) {
       throw new ArgumentException("Both arrays must be the same type");
   }
   
   // Create resulting array
   System.Array result = System.Array.CreateInstance(
       Array1.GetType(), Array1.Length + Array2.Length);
   
   // Copy both arrays, end to end, in the resulting array
   Array1.CopyTo(result, 0);
   Array2.CopyTo(result, Array1.Length);
   
   // Return the combined arrays
   return result;
}

[sIGPIC]e[/sIGPIC]
Posted
As far as I know, your best bet is to manually combine the arrays...

Fantastic - thanks. I haven't tried the second method, but I'll give it a go tonight.

 

The 'CopyTo' method is the sort of thing I was looking for - I was thinking that I was going to have to enumerate the array elements and copy them individually (or work with some horrendous copymemory hack like I used to do for VBA :()

Posted

   // Create resulting array
   System.Array result = System.Array.CreateInstance(
       Array1.GetType(), Array1.Length + Array2.Length);
 

Hmm. For some reason, that's creating an instance of {byte[][]} type array, when passed a {byte[]} array. Not sure why - the debugger is quite clearly showing that Array1 type is {byte[]}.

 

Maybe I should bo it explicitly and overload the JoinArray function for the different types that I might be using?

  • Leaders
Posted

Yeah, so createinstance will attempt to create an array of byte arrays instead of an array of bytes...

Perhaps, you'll want to do Array1.Item(0).GetType or something similar.

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted
Yeah, so createinstance will attempt to create an array of byte arrays instead of an array of bytes...

Perhaps, you'll want to do Array1.Item(0).GetType or something similar.

Yeah - finally realised that!

 

System.Array result = System.Array.CreateInstance(Array1.GetValue(0).GetType(), Array1.Length + Array2.Length);

 

(and a check that there is actually and element(0) in the array....

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...