View Single Post
 
Old 04-05-2013, 07:25 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Focus on this bit of code:
Code:
Dim j As Integer = 4
For i As Integer = 0 To bufferTemp.Length - 1
    buffersend(j) = bufferTemp(i)
    j += i
Next i
Think about what the indexes might be on each loop. Let's say bufferTemp has 10 bytes in it. That means buffersend will have 15 bytes in it. (10 + 4 = 14, the number you give an array sets the upper bound, not the size, so 0-14 is 15 elements.)

So keep in mind the biggest i should ever get is 9, and the biggest j should get is 15.
  • On the first iteration, i = 0 and j = 4. Then j := (4 + 0) = 4.
  • Second: i = 1, j = 4. j := (4 + 1) = 5
  • Third: i = 2, j = 5; j := (5 + 2) = 7
  • Fourth: i = 3, j = 7; j := (7 + 3) = 10
  • Fifth: i = 4, j = 10; j := (10 + 4) = 14
  • Sixth: i = 5, j = 14; j := (14 + 5) = 19
I think you meant j += 1, but I would've wrote the loop like this:
Code:
For i As Integer = 0 To bufferTemp.Length - 1
    buffersend(i + 4) = bufferTemp(i)
    j += i
Next i
Alternately, there are overloads of Array.Copy() that will safely copy an array.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote