Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

C# how to convert from double[][] to double[,]

Hello,

I was given a method that needs the input argument to be double[ , ].

Currently the data is stored a two dimensional array for another section of the application.

I haven't been able to reformat the data acording to the double [ , ]  data structure.

Where am I going wrong?

Thanks,

Here's my code:

                seriesArray = new double[numberOfSeries][];
                actualArray = new double[numberOfSeries][];

                for (int i = 0; i < numberOfSeries; i++)
                {
                    seriesArray[i] = new double[list.Count];

                    actualArray[i] = new double[list.Count];
                }

               double[,] data = new double[,] { };

                for (int i = 0; i < list.Count; i++)  
                {
                    actualArray[i] = GetValuesByName(list[i]); 

                    double[] actualValues = actualArray[i]; 

                    for (int j = 0; j < seriesArray[i].Length; j++) 
                    {
                        seriesArray[j][i] = (double)actualValues.GetValue(j);
                        data.SetValue((double)actualSeriesValues.GetValue(j), i); // this isn't working!
                        
                    }
                }

Open in new window

0
AndyC1000
Asked:
AndyC1000
2 Solutions
 
AndyC1000Author Commented:
The error is "System.ArgumentException was unhandled"
  Message=Array was not a one-dimensional array.
  Source=mscorlib
  StackTrace:
       at System.Array.SetValue(Object value, Int32 index)
     
0
 
hosneylkCommented:
the SetValue method needs two dimensions as the array is two-dimensional.

e.g.

data.SetValue((double)actualSeriesValues.GetValue(j), i, j);

and also the data array will have to be initialized with a size since right now you're initializing an empty array.

e.g.

double[,] data = new double[list.Count, list.Count];
0
 
crysallusCommented:
You've only passed a single index argument, so it's expecting a single dimensional array. I think you would want to include both index values like this (untested):

data.SetValue((double)actualSeriesValues.GetValue(j), i, j);

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now