VB.NET Array.Copy Question
-
Friends, I am trying to merge to byte arrays and can't seem to get my code to work. Code below. Can i get some thoughts on what might be happening? I have verified that the arrays DO have data in them(aka length > 0).
Dim pdfBytesSyllabus As Byte() = pdf.GetPdfBytesFromHtmlStream(streamSyllabus, System.Text.Encoding.ASCII)
Dim pdfBytesCenter As Byte() = pdf.GetPdfBytesFromHtmlStream(streamCenter, System.Text.Encoding.ASCII)
Array.Copy(pdfBytesCenter, 0, pdfBytesSyllabus, pdfBytesSyllabus.Length, pdfBytesCenter.Length)Eric
-
Friends, I am trying to merge to byte arrays and can't seem to get my code to work. Code below. Can i get some thoughts on what might be happening? I have verified that the arrays DO have data in them(aka length > 0).
Dim pdfBytesSyllabus As Byte() = pdf.GetPdfBytesFromHtmlStream(streamSyllabus, System.Text.Encoding.ASCII)
Dim pdfBytesCenter As Byte() = pdf.GetPdfBytesFromHtmlStream(streamCenter, System.Text.Encoding.ASCII)
Array.Copy(pdfBytesCenter, 0, pdfBytesSyllabus, pdfBytesSyllabus.Length, pdfBytesCenter.Length)Eric
Array.Copy won't work as it does not resize the destination array (pdfBytesSyllabus) to hold the additional data from pdfBytesCenter. The steps required are 1) Create a new array of sufficient length to hold both arrays 2) Copy pdfBytesSyllabus to the new array at index 0 3) Copy pdfBytesCenter to the new array at index pdfBytesSyllabus.Length Much the same thing could be done with a generic List Of Byte, which is really just an implementation of a resizable array with a lot of useful methods added.
-
Friends, I am trying to merge to byte arrays and can't seem to get my code to work. Code below. Can i get some thoughts on what might be happening? I have verified that the arrays DO have data in them(aka length > 0).
Dim pdfBytesSyllabus As Byte() = pdf.GetPdfBytesFromHtmlStream(streamSyllabus, System.Text.Encoding.ASCII)
Dim pdfBytesCenter As Byte() = pdf.GetPdfBytesFromHtmlStream(streamCenter, System.Text.Encoding.ASCII)
Array.Copy(pdfBytesCenter, 0, pdfBytesSyllabus, pdfBytesSyllabus.Length, pdfBytesCenter.Length)Eric
Hi Eric Whitmore Might possible u r making mistake in defining the number of items to be copied. Kindly review the the following link. http://msdn.microsoft.com/en-us/library/y5s0whfd.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2[^]
-
Hi Eric Whitmore Might possible u r making mistake in defining the number of items to be copied. Kindly review the the following link. http://msdn.microsoft.com/en-us/library/y5s0whfd.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2[^]
Uhhh...no. Alan nailed the problem exactly. You're just guessing at it.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Friends, I am trying to merge to byte arrays and can't seem to get my code to work. Code below. Can i get some thoughts on what might be happening? I have verified that the arrays DO have data in them(aka length > 0).
Dim pdfBytesSyllabus As Byte() = pdf.GetPdfBytesFromHtmlStream(streamSyllabus, System.Text.Encoding.ASCII)
Dim pdfBytesCenter As Byte() = pdf.GetPdfBytesFromHtmlStream(streamCenter, System.Text.Encoding.ASCII)
Array.Copy(pdfBytesCenter, 0, pdfBytesSyllabus, pdfBytesSyllabus.Length, pdfBytesCenter.Length)Eric
As Alan has said, the problem is to do with the fact that the array isn't big enough to contain this. A simple way to fix this is to create a generic list of bytes and add the data into it. You can easily get an array out of this like this:
Dim pdfData As New List(Of [Byte])(pdfBytesSyllabus.Length + pdfBytesCenter.Length)
pdfData.AddRange(pdfBytesCenter)
pdfData.AddRange(pdfBytesSyllabus)
' To get an array, just use
Dim cumulativePdfData As Byte() = pdfData.ToArray()I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier -
As Alan has said, the problem is to do with the fact that the array isn't big enough to contain this. A simple way to fix this is to create a generic list of bytes and add the data into it. You can easily get an array out of this like this:
Dim pdfData As New List(Of [Byte])(pdfBytesSyllabus.Length + pdfBytesCenter.Length)
pdfData.AddRange(pdfBytesCenter)
pdfData.AddRange(pdfBytesSyllabus)
' To get an array, just use
Dim cumulativePdfData As Byte() = pdfData.ToArray()I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easierThanks Alan/Pete, Code worked great!
Eric
-
Thanks Alan/Pete, Code worked great!
Eric
I'm glad I could help.
I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier