views:

481

answers:

5

I have a class with a public array of byte(). Lets say its Public myBuff as byte() Events within the class get chunks of data in byte() array. How do i tell the event code to stick the get chunk on the end. Lets say Private Sub GetChunk Dim chunk as byte ... get stuff in chunk Me.myBuff += chunk (stick chunk on end of public array End sub

Or am I totally missing the point?

+1  A: 

if i remember right, in vb you want to redim with preserve to grow an array.

Darren Kopp
but can you do that with an array defined as a public item in the class ? i.e. can you use the erserved words piblic and redim together ?
You can, but the performance sucks, class or no class.
Joel Coehoorn
ReDim Preserve yourArray(newSize)
David Sokol
A: 

You'll be constantly using the ReDim keyword, which is extremely inefficient.

Are you using .Net? If so, consider using a System.Collections.Generic.List(Of Byte) instead. You can use it's .AddRange() method to append your bytes, and it's .ToArray() method to get an array back out if you really need one.

Joel Coehoorn
List(Of Byte) seems to have some size limitations. It can't go above a certain threshold, while the ArrayList can.
David Sokol
List(Of Byte)'s Count and Capacity properties are Int32s, same as ArrayList. ArrayList will box every byte you put in it, adding masses of GC and memory overhead. Perhaps you ran into a virtual memory fragmentation problem? Rare to get a 2GB free block of VM.
Mike Dimmick
I don't believe so. I was doing it all on a x64 box. The arraylist shot up to around 3.5gb before I killed it, while the List died around 900mb.
David Sokol
A: 

Your question doesn't seem to be very clear. You should probably not have the array of bytes as public. It should probably be private and you should provide a set of public functions that allow users of the class to perform operations against the array.

Sam
A: 

I think you might be looking for something other then an array. If you are trying to gradually extend the amount of data frequently, you should use a dynamic data structure such asArrayList. This has an Add method which adds the specific object or value to the array without concerns for space. It also has a nifty ToArray() method that you can use.

If you are trying to use an array for specific reasons (performance, I guess), use ReDim Preserve array(newSize).

David Sokol
+1  A: 

If the array is small, and new data is infrequently added, an easy way would be to:

public BufferSize as long 'or you can just use Ubound(mybuff), I prefer a tracker var tho
public MyBuff

private sub GetChunk()
dim chunk as byte
'get stuff
BufferSize=BufferSize+1

redim preserve MyBuff(buffersize)
mybuff(buffersize) = chunk
end sub

if chunk is an array of bytes, it would look more like:

buffersize=buffersize+ubound(chunk) 'or if it's a fixed-size chunk, just use that number
redim preserve mybuff(buffersize)
for k%=0 to ubound(chunk) 'copy new information to buffersize
  mybuff(k%+buffersize-ubound(chunk))=chunk(k%)
next

if you will be doing this frequently (say, many times per second) you'd want to do something like how the StringBuilder class works:

public BufSize&,BufAlloc& 'initialize bufalloc to 1 or a number >= bufsize
public MyBuff() as byte

sub getdata()
bufsize=bufsize+ubound(chunk)
if bufsize>bufalloc then
  bufalloc=bufalloc*2
  redim preserve mybuff(bufalloc)
end if
for k%=0 to ubound(chunk) 'copy new information to buffersize
  mybuff(k%+bufsize-ubound(chunk))=chunk(k%)
next
end sub

that basically doubles the memory allocated to mybuf each time the pointer passes the end of the buffer. this means much less shuffling around of memory.