Convert a int[] to a double[]

A

AMP

Hello,
I am passing a int[] to a function that only takes a double[]

private double[] GraphData;
public Form3(int[] OurData)
{
GraphData = OurData;
foreach (int x in GraphData)
{

System.Convert.ToDouble(x);
}

InitializeComponent();
}

private void Form3_Load(object sender, EventArgs e)
{
CreateGraph(zedGraphControl1,GraphData);
}



...................

double[] y = GraphData;



It gives me errors.
How can I pass an int Array to a form constructor and then convert it
to a double Array?
Thanks
Mike
 
C

CSharper

Hello,
I am passing a int[] to a function that only takes a double[]

        private double[] GraphData;
        public Form3(int[] OurData)
        {
            GraphData = OurData;
            foreach (int x in GraphData)
            {

                System.Convert.ToDouble(x);
            }

            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            CreateGraph(zedGraphControl1,GraphData);
        }

..................

double[] y = GraphData;

It gives me errors.
How can I pass an int Array to a form constructor and then convert it
to a double Array?
Thanks
Mike

Try the following

private double[] GraphData;
public Form3(int[] OurData)
{
GraphData = new double[ourData.Length];
int idx=0;
foreach (int x in GraphData)
{
GraphData[idx++] = System.Convert.ToDouble(x);
}
InitializeComponent();
}


private void Form3_Load(object sender, EventArgs e)
{
CreateGraph(zedGraphControl1,GraphData);
}
 
C

CSharper

Hello,
I am passing a int[] to a function that only takes a double[]
        private double[] GraphData;
        public Form3(int[] OurData)
        {
            GraphData = OurData;
            foreach (int x in GraphData)
            {
                System.Convert.ToDouble(x);
            }
            InitializeComponent();
        }
        private void Form3_Load(object sender, EventArgs e)
        {
            CreateGraph(zedGraphControl1,GraphData);
        }
..................

double[] y = GraphData;
It gives me errors.
How can I pass an int Array to a form constructor and then convert it
to a double Array?
Thanks
Mike

Try the following

private double[] GraphData;
        public Form3(int[] OurData)
        {
            GraphData = new double[ourData.Length];
           int idx=0;
            foreach (int x in GraphData)
            {
                GraphData[idx++] = System.Convert.ToDouble(x);
            }
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            CreateGraph(zedGraphControl1,GraphData);
        }- Hide quoted text -

- Show quoted text -

Sorry correction;

public Form3(int[] OurData)
{
GraphData = new double[ourData.Length];
int idx=0;
foreach (int x in OurData)
{
GraphData[idx++] = System.Convert.ToDouble(x);
}
InitializeComponent();
}
 
P

Peter Morris

int[] integerArray = new int[] { 1, 2, 3 };
List<double> doubleList = new List<double>();
foreach (int i in integerArray)
doubleList.Add((double)i);

double[] doubleArray = doubleList.ToArray();




or using Linq


int[] integerArray = new int[] { 1, 2, 3 };
double[] doubleArray =
(from currentValue in integerArray select (double)currentValue).ToArray();
 
G

Gilles Kohl [MVP]

Peter,

int[] integerArray = new int[] { 1, 2, 3 };
List<double> doubleList = new List<double>();
foreach (int i in integerArray)
doubleList.Add((double)i);

double[] doubleArray = doubleList.ToArray();




or using Linq


int[] integerArray = new int[] { 1, 2, 3 };
double[] doubleArray =
(from currentValue in integerArray select (double)currentValue).ToArray();

Wouldn't

double [] doubleArray = integerArray.Cast<double>().ToArray();

do too?

Regards,
Gilles.
 
C

CSharper

int[] integerArray = new int[] { 1, 2, 3 };
List<double> doubleList = new List<double>();
foreach (int i in integerArray)
 doubleList.Add((double)i);
double[] doubleArray = doubleList.ToArray();

I think I'd pre-allocate the List<T> in this case:

     List said:
or using Linq
int[] integerArray = new int[] { 1, 2, 3 };
double[] doubleArray =
 (from currentValue in integerArray select  
(double)currentValue).ToArray();

That works.  Or this too:

     double[] doubleArray = integerArray.Cast<double>().ToArray();

Pete

I really like what linq brings to C#3.0.
 
J

Jon Skeet [C# MVP]

AMP said:
I am passing a int[] to a function that only takes a double[]

You can convert an int array into a double array like this:

GraphData = Array.ConvertAll(OurData,
delegate(int x) { return (double)x; }
);

Or in C# 3:

GraphData = Array.ConvertAll(OurData, x => (double)x);
 
P

Peter Morris

Currently I mainly use linq to populate ViewData in a website without giving
access to the objects themselves.....

ViewData["OrderLines"] =
from line in order.Lines
select new
{
LineNumber = line.LineNumber,
ProductName = line.Product.Name,
QuantityOrdered = line.QuantityOrdered,
etc = line.etc
};


:)



Pete
 

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