471,319 Members | 3,253 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Efficiently convert Base 1 Array to Base 0 Array?

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

Feb 8 '06 #1
9 6955
Hi,

"AtariPete" <pc******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Feb 8 '06 #2
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?

Feb 8 '06 #3
Hi,

"AtariPete" <pc******@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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 ?

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Feb 8 '06 #4
>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.

Feb 8 '06 #5
Look into Buffer.BlockCopy. It's a lot faster than going through each item
and assigning it to another array.
"AtariPete" <pc******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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.

Feb 8 '06 #6
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
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


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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 8 '06 #7
Jon Skeet [C# MVP] wrote:
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
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

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
Feb 9 '06 #8
Scott C <sdcoonce@g_m_a_i_l.com> wrote:
Nop, there is no thing like a base index, ALL arrays in C# start at index 0


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 :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 9 '06 #9
Jon Skeet [C# MVP] wrote:

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 32F 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
Feb 10 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by danc | last post: by
4 posts views Thread by kvicky | last post: by
5 posts views Thread by Maxim | last post: by
12 posts views Thread by Peter | last post: by
reply views Thread by rosydwin | last post: by

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.