Re: Combining 2 Byte Arrays
Hey rkeslar, I was wondering the same thing last night. Your code should look like this:
vb.net Code:
Dim bytearray3() As Byte = bytearray1.Concat(bytearray2).ToArray()
Re: Combining 2 Byte Arrays
Quote:
Originally Posted by
tassa
Hey rkeslar, I was wondering the same thing last night. Your code should look like this:
vb.net Code:
Dim bytearray3() As Byte = bytearray1.Concat(bytearray2).ToArray()
How did you get Concat to come up in Intellisense? I get an error that says Concat is not a member of System.Array. Did you have to import a namespace?
Re: Combining 2 Byte Arrays
Concat is actually an extension only useable in .NET 3.5 and up
Re: Combining 2 Byte Arrays
Oh that sucks. Wish I was working in 3.5 now.
Re: Combining 2 Byte Arrays
This is probably more or less what Concat does anyway:
vb.net Code:
Public Shared Function Concat(Of TSource)(ByVal first As IEnumerable(Of TSource), ByVal second As IEnumerable(Of TSource)) As IEnumerable(Of TSource)
If first Is Nothing Then Throw New NullReferenceException("Parameter 'first' cannot be null.")
If second Is Nothing Then Throw New NullReferenceException("Parameter 'second' cannot be null.")
Dim concatenation As New List(Of TSource)(first)
concatenation.AddRange(second)
Return CType(concatenation, IEnumerable(Of TSource))
End Function
Re: Combining 2 Byte Arrays
What is TSource? I get an error that says 'System.Collections.IEnumerable' has no type parameters and so cannot have type arguments.'
Also, did you have to import a namespace to declare concatenation as a New List?
Re: Combining 2 Byte Arrays
TSource is a generic. You will probably want to do some research on that to learn exactly what it entails. I feel like I'm rewriting LINQ:
vb.net Code:
Public Sub SomeMethod()
Dim firstArray() As String = {"first", "array", "values"}
Dim secondArray() As String = {"second", "array", "values", "also"}
Dim concatenation() As String = ToArray(Concat(firstArray, secondArray))
For Each element As String In concatenation
MessageBox.Show(element)
Next
End Sub
Public Shared Function Concat(Of TSource)(ByVal first As IEnumerable(Of TSource), ByVal second As IEnumerable(Of TSource)) As IEnumerable(Of TSource)
If first Is Nothing Then Throw New NullReferenceException("Parameter 'first' cannot be null.")
If second Is Nothing Then Throw New NullReferenceException("Parameter 'second' cannot be null.")
Dim concatenation As New List(Of TSource)(first)
concatenation.AddRange(second)
Return CType(concatenation, IEnumerable(Of TSource))
End Function
Public Shared Function ToArray(Of TSource)(ByVal source As IEnumerable(Of TSource)) As TSource()
If source Is Nothing Then Throw New NullReferenceException("Parameter 'source' cannot be null.")
Dim listTSource As New List(Of TSource)(source)
Return listTSource.ToArray()
End Function
I used strings in the example, but you can use anything you want. This is what a generic parameter (TSource) allows you to do. You can now use those Concat and ToArray functions for object that implements IEnumerable(Of T).
Re: Combining 2 Byte Arrays
Why not just use a List(Of Byte), call AddRange to add the 2 byte arrays to it and then call ToArray to have the final output array? The same idea as FA has, but his code is way overkills.
Re: Combining 2 Byte Arrays
You can do this:
vb.net Code:
Dim _byte As Byte() = {1, 2, 3}
Dim _2byte As Byte() = {4, 5, 6}
Dim length As Integer = _byte.Length + _2byte.Length
'_newByte is a new byte array that will hold _
'the concatenation of _byte and _2byte
Dim _newByte As Byte() = New Byte(length) {}
_byte.CopyTo(_newByte, 0)
_2byte.CopyTo(_newByte, _byte.Length)