Efficiently convert Base 1 Array to Base 0 Array?

A

AtariPete

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

AtariPete said:
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.

Just for your clarification, the "start" of the array is something
artificial based on the language used, in reality what you have is a
continuos chunk of memory divided in N equals parts where each part hold an
element of the array.
The index is used to indicate the compiler the offset from the start to get
the element you want. therefore is "artificial" , what I mean is that to
access the first element in that very same array you use index 0 or 1
depending of the language you use.

IIRC in some old version of pascal (or was basic ) you could even define
the start index of the array. it could be negative !


you do not need to create a new array and copy it.
 
A

AtariPete

Hi Ingacio,
Just for your clarification, the "start" of the array is something
artificial based on the language used

This I get. In memory, two arrays that have a different base index but
the same content are really not different.

I feel the issue at hand is that I am working in a managed environment
where memory address is kept hidden ( I do not want to use "unsafe"
code). As a result, i have yet to find an elegant a solution.

Consider two int arrays (int[] x and int[] y) where x has a base index
of 1 and y has a base index of 0.

How do i convert from x to y?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

AtariPete said:
Hi Ingacio,
artificial based on the language used
This I get. In memory, two arrays that have a different base index but
the same content are really not different.

Nop, there is no thing like a base index, ALL arrays in C# start at index 0
I feel the issue at hand is that I am working in a managed environment
where memory address is kept hidden ( I do not want to use "unsafe"
code). As a result, i have yet to find an elegant a solution.

You do not need the memory address, when you receive your memory chunk from
the COM object it will be ready to be accessed as array[0] to get thr first
element.
Consider two int arrays (int[] x and int[] y) where x has a base index
of 1 and y has a base index of 0.

There is no thing as an array with base 1 in C# , once you accept this
everything will be clearer.

how are you receiving the array frmo the COM object? can you post the code ?
 
A

AtariPete

There is no thing as an array with base 1 in C# , once you accept this
everything will be clearer.

I know there base 1 arrays do not exist in C#. But they can be handled
in C#. No?
how are you receiving the array frmo the COM object?

I am using Microsoft's owc10 excel component. I am accessing a range of
data in an owc instance. This range is returned as an object. This
object is actually a 1 based object array (object[,]) of objects which
are actually strings. I am passing this data to a component that
required the array to be 0 based. This is why I need to convert from 0
based to 1 based.
 
R

Rocky

Look into Buffer.BlockCopy. It's a lot faster than going through each item
and assigning it to another array.


AtariPete said:
There is no thing as an array with base 1 in C# , once you accept this
everything will be clearer.

I know there base 1 arrays do not exist in C#. But they can be handled
in C#. No?
how are you receiving the array frmo the COM object?

I am using Microsoft's owc10 excel component. I am accessing a range of
data in an owc instance. This range is returned as an object. This
object is actually a 1 based object array (object[,]) of objects which
are actually strings. I am passing this data to a component that
required the array to be 0 based. This is why I need to convert from 0
based to 1 based.
 
J

Jon Skeet [C# MVP]

<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us> said:
Nop, there is no thing like a base index, ALL arrays in C# start at index 0

Hmm... that's only sort of true.

1) You can certainly have instances of System.Array (rather than C#
arrays) with a non-zero base
2) You can have multidimensional arrays (created using
Array.CreateInstance) which can be cast to (say) int[,] and used as
normal, even with non-zero bases

It's all a little bit odd, unfortunately.

The C# specification claims that C# arrays (as opposed to .NET arrays)
are always 0-based, but as I say with multidimensional arrays that's
not always true...
 
S

Scott C

Jon said:
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT



Hmm... that's only sort of true.

In principle, I agree, Jon. But that's not what the OP originally
wanted. He said he had "(int[] x and int[] y)" which were ONE based
since they came from (for all intents and purposes) outside CSharp.
Ignacio's point is that the "base" of an int[] in CSharp is *always*
zero. So when the OP says that the array is ONE-based makes absolutely
no sense-- an array doesn't have a sense of "based"-ness.

I'm not sure I'm making myself understood-- I'll stop there.

Scott
 
J

Jon Skeet [C# MVP]

Scott C said:
Hmm... that's only sort of true.

In principle, I agree, Jon. But that's not what the OP originally
wanted. He said he had "(int[] x and int[] y)" which were ONE based
since they came from (for all intents and purposes) outside CSharp.
Ignacio's point is that the "base" of an int[] in CSharp is *always*
zero. So when the OP says that the array is ONE-based makes absolutely
no sense-- an array doesn't have a sense of "based"-ness.

I'm not sure I'm making myself understood-- I'll stop there.

An array does, but an int[] doesn't. An int[] is always zero-based, but
an int[,] isn't, and a System.Array isn't. I was only commenting for
the sake of precision, really :)
 
S

Scott C

Jon said:
An array does, but an int[] doesn't. An int[] is always zero-based, but
an int[,] isn't, and a System.Array isn't. I was only commenting for
the sake of precision, really :)
Just to beat a dead horse, an int[] isn't zero based, necessarily. In
CSharp it is, and in lots of other languages it is, but it's just a
convention. Kind of like calling 32°F freezing. The freezing point of
water is no more 32-indexed than int[] is zero indexed, it's just the
convention for a country or a compiler. (In this non-zero indexed
sense, I would say the USA has more in common with VB, and the rest of
the world is more akin to CS.)

The question would better be phrased as: given a list of numbers,
say 9, 4, 7, 1
how do we call these using an index. Since most would agree that it
makes sense that then _next_ number is index+1, the only real question
is how does the compiler "address" the number 9? --My point is that the
"list" doesn't change if 9 is addressed by 0, 2, 1.643 or 5.3e10.

OK, I think the horse is now sufficiently dead, I feel I can rest quietly.

Scott
 

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