Efficiently convert Base 1 Array to Base 0 Array?

P

Peter Nofelt

Hi all,

My scenario is as follows:

* Receive a base 1 array (index starts at 1 instead of 0) of data from
a COM component .
* Need to pass this array to a .net component that requires its array
index to start at 0.
* As a result, must convert array from base1 to base 0.

Currently my solution for this is to create a new array and copy the
contents of is as follows:
* Create new based 0 array
* Copy the content of base 1 array to new array by 1)iterating through
each element in base 1 array and 2)doing assignment to base 0 array.
* Assign new array to array used by .net component described above.

My question is as follows:
Is there a quicker way to convert a base1 array to base0 without
having to do iteration and assignment? Does something in the .net
framework already do this?

Thanks,
Peter
www.nofelt.com
 
C

Chris Dunaway

Peter said:
* Receive a base 1 array (index starts at 1 instead of 0) of data from
a COM component .
* Need to pass this array to a .net component that requires its array
index to start at 0.
* As a result, must convert array from base1 to base 0.

The indexes are not passed with the array. When the array is received
from the COM component, its first element should be referred to with
index 0 in .Net.

Does the array have an *empty* element at index 0 when you get it in
..Net?
Currently my solution for this is to create a new array and copy the
contents of is as follows:
* Create new based 0 array
* Copy the content of base 1 array to new array by 1)iterating through

There is no such thing as a "0 based array" or a "1 based array" unless
I am missing something. When you receive the array in .Net, addressing
the element with index 0 should be the first element in the array. You
should not have to convert anything. Unless the array as received from
COM has an *empty* element at index 0.
My question is as follows:
Is there a quicker way to convert a base1 array to base0 without
having to do iteration and assignment? Does something in the .net
framework already do this?

You shouldn't have to do anything. Someone please enlighten me if I am
wrong.
 
C

Chuck Gantz

The usual problem is that while the array is just a contiguous section of
memory and so there is no idea of whether the first element has index 0 or 1
or something else, the algorithms where the array is used does have a
preference.

See http://www.library.cornell.edu/nr/bookcpdf/c1-2.pdf for the Numerical
Recipes in C discussion of this, and a solution in C.

Chuck Gantz
 
J

Jon Skeet [C# MVP]

Chris Dunaway said:
There is no such thing as a "0 based array" or a "1 based array" unless
I am missing something. When you receive the array in .Net, addressing
the element with index 0 should be the first element in the array. You
should not have to convert anything. Unless the array as received from
COM has an *empty* element at index 0.

..NET itself certainly *does* have non-zero based arrays. Look at the
various overloads to Array.CreateInstance for details. C# won't let you
use a non-zero based single dimensional array as MyType[] but it will
let you use multi-dimensional arrays as MyType[,] etc whatever their
bases are.
 
C

Chris Dunaway

Agreed, but his COM component isn't returning an algorithm. It's
returning an array. Whether or the COM component accessed the array
using 1 or 0 as the base index is irrelevant. Once it gets to the .Net
side, he can use 0 based indexing without having to convert the array
further.

I guess if the index had some significance to the actual data stored in
the array, he might have to make appropriate adjustments, but for just
accessing the elements of the array, he should be able to start with
index 0.
 
C

Chris Dunaway

Yes, but whatever the base is, its just making a calculation to find
the offset in the array for that index. If the COM control is
returning an array, the 'base' of the array is not returned. The op
should be able to just use standard '0 based' indexes to refer to the
elements in the array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top