Legend Posted December 8, 2005 Posted December 8, 2005 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. Quote
Leaders snarfblam Posted December 8, 2005 Leaders Posted December 8, 2005 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; } Quote [sIGPIC]e[/sIGPIC]
Legend Posted December 8, 2005 Author Posted December 8, 2005 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 :() Quote
Legend Posted December 8, 2005 Author Posted December 8, 2005 // 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? Quote
Leaders Iceplug Posted December 8, 2005 Leaders Posted December 8, 2005 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. Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
Legend Posted December 8, 2005 Author Posted December 8, 2005 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.... Quote
Leaders snarfblam Posted December 9, 2005 Leaders Posted December 9, 2005 Oops... my bad. Quote [sIGPIC]e[/sIGPIC]
Legend Posted December 9, 2005 Author Posted December 9, 2005 Oops... my bad. Well, I still appreciate the input - and it helps me learn when I have to actually work out for myself what's going wrong! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.