Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Convert a int[] to a double[]

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AMP - 25 Mar 2008 20:34 GMT
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
CSharper - 25 Mar 2008 20:49 GMT
> Hello,
> I am passing a int[] to a function that only takes a double[]
[quoted text clipped - 26 lines]
> 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);
       }
CSharper - 25 Mar 2008 20:50 GMT
> > Hello,
> > I am passing a int[] to a function that only takes a double[]
[quoted text clipped - 47 lines]
>
> - 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();
        }
Peter Morris - 25 Mar 2008 20:53 GMT
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();
Gilles Kohl [MVP] - 25 Mar 2008 21:04 GMT
Peter,

>int[] integerArray = new int[] { 1, 2, 3 };
>List<double> doubleList = new List<double>();
[quoted text clipped - 8 lines]
>double[] doubleArray =
> (from currentValue in integerArray select (double)currentValue).ToArray();

Wouldn't

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

do too?

  Regards,
  Gilles.
Jon Skeet [C# MVP] - 25 Mar 2008 21:23 GMT
> Wouldn't
>
>   double [] doubleArray = integerArray.Cast<double>().ToArray();
>
> do too?

I believe it would right now, but it shouldn't, and it won't with .NET
3.5 SP1.

http://blogs.msdn.com/ed_maurer/archive/2008/02/16/breaking-change-in-
linq-queries-using-explicitly-typed-range-variables.aspx

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Peter Duniho - 25 Mar 2008 21:46 GMT
> I believe it would right now, but it shouldn't, and it won't with .NET
> 3.5 SP1.
>
> http://blogs.msdn.com/ed_maurer/archive/2008/02/16/breaking-change-in-
> linq-queries-using-explicitly-typed-range-variables.aspx

Ah.  I was wondering about that.  :)  I was actually surprised when I  
wrote my little test code to see if Cast<T> worked to find that it did.  
Before the test, I'd assumed it would literally just try to cast to the  
target type.

I was pleasantly surprised that it did more than that.

Of course, that happiness was short-lived.  Thanks for pulling the rug  
out.  :)

Pete
Gilles Kohl [MVP] - 25 Mar 2008 22:34 GMT
>> Wouldn't
>>
[quoted text clipped - 7 lines]
>http://blogs.msdn.com/ed_maurer/archive/2008/02/16/breaking-change-in-
>linq-queries-using-explicitly-typed-range-variables.aspx

Good to know Jon, thanks for the pointer.

  Regards,
  Gilles.
Peter Duniho - 25 Mar 2008 21:12 GMT
> 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<double> doubleList = new List<double>(integerArray.Length);

> 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
CSharper - 25 Mar 2008 21:17 GMT
On Mar 25, 3:12 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> On Tue, 25 Mar 2008 12:53:41 -0700, Peter Morris <peter[dot]morris  
>
[quoted text clipped - 22 lines]
>
> Pete

I really like what linq brings to C#3.0.
Peter Duniho - 25 Mar 2008 21:24 GMT
> I really like what linq brings to C#3.0.

Me too.  Though, ironically, I have found that for my own purposes,  
because I am so rarely writing any code that uses a database, I'm never  
using the database-oriented aspects of LINQ.

Even though my first exposure to it was in situations specifically  
referencing database manipulation, and even though the power of LINQ  
appears to be most directly applicable in that context, and even though  
the examples I continue to be exposed to are most often in that context,  
it turns out it's very useful for other more mundane things as well.  :)

But yes, it's a very nice addition, as is the extension methods feature  
that makes much of what's in LINQ possible (especially the stuff I'm using  
:) ).

Pete
Peter Morris - 26 Mar 2008 10:01 GMT
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
Jon Skeet [C# MVP] - 25 Mar 2008 21:24 GMT
<snip>

> That works.  Or this too:
>
>      double[] doubleArray = integerArray.Cast<double>().ToArray();

As I replied to Gilles (just repeating myself as this could be a
dangerously seductive answer otherwise) - that works now, but will
probably break with .NET 3.5 SP1.

http://blogs.msdn.com/ed_maurer/archive/2008/02/16/breaking-change-in-
linq-queries-using-explicitly-typed-range-variables.aspx

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Jon Skeet [C# MVP] - 25 Mar 2008 21:18 GMT
> 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);

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.