Byte array access

  • Thread starter Thread starter Denis C
  • Start date Start date
D

Denis C

Hi

I have a byte array of length 2404. I want to access
subsets of the array and pass the subset to the
constructor of a class.

i.e.

Dim bData(2403) As Byte
Dim aThing As SomeClass = new SomeClass(bData(12..211))

where bData(12..211) passes bytes 12 to 211 from bData.

Is there some way of doing this?

Denis
 
Denis C said:
Hi

I have a byte array of length 2404. I want to access
subsets of the array and pass the subset to the
constructor of a class.

i.e.

Dim bData(2403) As Byte
Dim aThing As SomeClass = new SomeClass(bData(12..211))

where bData(12..211) passes bytes 12 to 211 from bData.

Is there some way of doing this?

No.

You could pass a new array containing a copy of the items in the old
array. Or you have to pass the array and the start- and endindex. Or: Write
a wrapper class wrapping the array. Something like this:

Class WrappedArray
Private m_Array As Byte()
Private m_StartIndex, m_EndIndex As Integer

Public Sub New( _
ByVal b As Byte(), ByVal StartIndex As Integer, _
ByVal EndIndex As Integer)

m_Array = b
m_StartIndex = StartIndex
m_EndIndex = EndIndex
End Sub
Default Public Property Item(ByVal Index As Integer) As Byte
Get
Index = GetIndex(Index)
Return m_Array(Index)
End Get
Set(ByVal Value As Byte)
Index = GetIndex(Index)
m_Array(Index) = Value
End Set
End Property
Private Function GetIndex(ByVal Index As Integer) As Integer
If Index < 0 OrElse Index > m_EndIndex - m_StartIndex Then
Throw New IndexOutOfRangeException
End If
Return m_StartIndex + Index
End Function
Public ReadOnly Property Count() As Integer
Get
Return m_EndIndex - m_StartIndex + 1
End Get
End Property

End Class
 
You can easily create a copy of the array with only the subset by using
Array.Copy() and supplying the starting index and length.

Or, you can pass the entire array to the constructor, and ALSO pass a
starting index and length. For example,
Dim aThing As New SomeClass(bData, 12, 98)
Then, you know in the constructor which elements you'll need to work with.
This has two advantages over copying the array subset:
1) you don't need to create a duplicate of the data (save some memory)
2) changes (if any) to the elements will also be reflected in the original
reference

-Rob Teixeira [MVP]
 
Denis,
Do you want the bData array itself to be modified by SomeClass, or does
SomeClass need a copy of the data?

You can use Array.Copy if SomeClass only needs a copy of the data.

If SomeClass needs a reference to the data you have two choices.
1. pass the array, index & length to SomeClass itself, and let SomeClass
handle the indexing into the original array.
2. Create an Array Proxy that you pass the array, index & length to.
SomeClass would accept the ArrayProxy as a parameter. The array Proxy would
handle the indexing into the original array.

Seeing as an array is an object on the heap, you automatically get the
reference in both cases.

In either case you can offer overloads for index & length, following how the
framework does it:

void MyFunction(Array array)
void MyFunction(Array array, int length)
void MyFunction(Array array, int index, int length)

Where index would default to 0, and length would default to the length of
the array.

Hope this helps
Jay
 
* "Denis C said:
I have a byte array of length 2404. I want to access
subsets of the array and pass the subset to the
constructor of a class.

i.e.

Dim bData(2403) As Byte
Dim aThing As SomeClass = new SomeClass(bData(12..211))

where bData(12..211) passes bytes 12 to 211 from bData.

Have a look at 'Array.Copy' and 'Array.CopyTo'. Alternatively, you can
pass the whole array and the start/end index of the subset in the whole
array.
 
Thanks for the replies. Went with passing the entire
array and the start index as the length was already
known.

Love this group, always very helpful!

Denis
 
Back
Top