Excel and C# cannot recast object[,] to double[,] or object[x,y] to int

C

cendter

Hi there,

I am utterly confused - I have a form that starts an instance from
excel and let's the user select a range. I then want to take this
range as an array of doubles. I ran into trouble because the 1-based
indexing that Excel passes (I think) so after many tries I got:

Excel.Range aRange =
xlApp.get_Range(textBox2.Text.Split(':')[0],textBox2.Text.Split(':')
[1]);
object[,] xacc = new object[aRange.Rows.Count,
aRange.Columns.Count];
xacc = (object[,])aRange.Value2;

to work ... now xacc is an object[,] array but it seems to be 1-
based? Does anyone know how this can be corrected to the normal 0-
based c# arrays.

More importantly, any conversion and casting for either the whole
array to, say, double[,] or just for a single element to double or int
fails with an exception 'Invalid cast'.

Can anyone help or provide an example of how to take a range and
convert it into a two dimensional array of doubles?

Thank you very much.

Sunrider
 
J

John B

Hi there,

I am utterly confused - I have a form that starts an instance from
excel and let's the user select a range. I then want to take this
range as an array of doubles. I ran into trouble because the 1-based
indexing that Excel passes (I think) so after many tries I got:

Excel.Range aRange =
xlApp.get_Range(textBox2.Text.Split(':')[0],textBox2.Text.Split(':')
[1]);
object[,] xacc = new object[aRange.Rows.Count,
aRange.Columns.Count];
xacc = (object[,])aRange.Value2;

to work ... now xacc is an object[,] array but it seems to be 1-
based? Does anyone know how this can be corrected to the normal 0-
based c# arrays.

More importantly, any conversion and casting for either the whole
array to, say, double[,] or just for a single element to double or int
fails with an exception 'Invalid cast'.

Can anyone help or provide an example of how to take a range and
convert it into a two dimensional array of doubles?

Thank you very much.

Sunrider
Remember you have to unbox to the exact type that is boxed (ex enums).
So if it is a decimal, you cannot unbox to double and vice versa.
If you dont know the type in advance then you can use
convert.changetype(...)
As for the cast from object[,] to double[,], a double[,] is not an
object[,] so it cannot be done, you will have to write a method to
create a new array of the correct type and base and copy the elements
manually.

JB
 
A

Alvin Bruney [MVP]

Does anyone know how this can be corrected to the normal 0-
based c# arrays.
It's legacy, 1 based. That part is set in stone.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET - MS Press
Professional VSTO 2005 - Wrox/Wiley
OWC Black Book www.lulu.com/owc

John B said:
Hi there,

I am utterly confused - I have a form that starts an instance from
excel and let's the user select a range. I then want to take this
range as an array of doubles. I ran into trouble because the 1-based
indexing that Excel passes (I think) so after many tries I got:

Excel.Range aRange =
xlApp.get_Range(textBox2.Text.Split(':')[0],textBox2.Text.Split(':')
[1]);
object[,] xacc = new object[aRange.Rows.Count,
aRange.Columns.Count];
xacc = (object[,])aRange.Value2;

to work ... now xacc is an object[,] array but it seems to be 1-
based? Does anyone know how this can be corrected to the normal 0-
based c# arrays.

More importantly, any conversion and casting for either the whole
array to, say, double[,] or just for a single element to double or int
fails with an exception 'Invalid cast'.

Can anyone help or provide an example of how to take a range and
convert it into a two dimensional array of doubles?

Thank you very much.

Sunrider
Remember you have to unbox to the exact type that is boxed (ex enums).
So if it is a decimal, you cannot unbox to double and vice versa.
If you dont know the type in advance then you can use
convert.changetype(...)
As for the cast from object[,] to double[,], a double[,] is not an
object[,] so it cannot be done, you will have to write a method to create
a new array of the correct type and base and copy the elements manually.

JB
 

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