473,385 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Byte array access

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
Nov 20 '05 #1
6 3161
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 C" <an*******@discussions.microsoft.com> wrote in message
news:02****************************@phx.gbl...
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

Nov 20 '05 #2
See the Array.Copy method.
"Denis C" <an*******@discussions.microsoft.com> wrote in message
news:02****************************@phx.gbl...
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

Nov 20 '05 #3
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" <an*******@discussions.microsoft.com> wrote in message
news:02****************************@phx.gbl...
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

Nov 20 '05 #4
* "Denis C" <an*******@discussions.microsoft.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #5
"Denis C" <an*******@discussions.microsoft.com> schrieb
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
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #6
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
-----Original Message-----
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
.

Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Ray Z | last post by:
So far, I get the idea that if I want to use both the unmanaged and managed memory, I can not avoid memory copy. But I DO need to avoid it. I get a idea that maybe I could use "union" to convert...
1
by: Andre Ranieri | last post by:
I'm having trouble programatically inserting an Excel file into an Image column in our CRM package's SQL 2000 database. The function appears to work ok, but when I attempt to access the file through...
4
by: Dennis Myrén | last post by:
Hi. Is there a way to utilize the great primitive data type formatting routines available in .NET without working with strings? I want a byte directly rather than a string. I think it is...
5
by: _BNC | last post by:
I've converted " byte" to "byte *" at times, using 'unsafe' and fixed { .... }, but the reverse does not seem to work. In this case, a C++ DLL returns a byte * and a length. What is the best...
6
by: Brian Keating EI9FXB | last post by:
Hello there, I've just come accross this and wonder if i'm taking the best approach? I read a byte array from the registry, this byte array is basically an array of Int32 so i wish to revert and...
2
by: Howard Weiss | last post by:
I am reading a file (containing short integers). To read the file, I use the following FileStream *myFile = new FileStream(FileName, FileMode::Open, FileAccess::Read); __int64 myFileSize =...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
24
by: ThunderMusic | last post by:
Hi, The subject says it all... I want to use a byte and use it as byte* so I can increment the pointer to iterate through it. What is the fastest way of doing so in C#? Thanks ThunderMusic
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.