Matthijs Wessels Matthijs Wessels - 11 months ago 94
C# Question

How can I access the values from `Series.Values` (Excel Chart)

I am using the

namespace and I am creating a chart. At a certain moment, I want to retrieve the values of a certain series. On MSDN it says that a
object has a property
. This returns either a
object or an
array of values
, I assume an
. In my code I have the following statement:

Series series = (Series)chart.SeriesCollection(i);
object[] values = (object[])series.Values;

I get an
with the message:
Unable to cast object of type 'System.Object[*]' to type 'System.Object[]'.

When I debug, using Visual Studio 2008, I can inspect the type of
and it says
. This means (as I understand) that it is declared as an
but its actual type is
. But
is not really a type I can cast to and neither is

I suspect (or guess) that it might have something to do with the fact that the array starts at 1 instead of 0 (probably because of VB). I didn't even know you could define a 1 based array in c#...

Answer Source

Even tough it might seem weird in C# to create non-zero index based arrays it is actually possible:

var array = Array.CreateInstance(
    new int[] { 7 }, 
    new int[] { 1 });

In your case I think you should be able to cast to an array and enumerate:

foreach (object item in (Array)series.Values)

And there's an interesting article explaining the saga around this types of arrays in the CLR.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download