Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Converting an array list to double array

Converting an array list to double array

Scheduled Pinned Locked Moved C#
questiondata-structures
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _Searcher_
    wrote on last edited by
    #1

    Hi! I'm having some problems in my code. I want to get values from a datareader to a double[]. I have read the values to an array list, this is my code: int counter = 0; ArrayList al = new ArrayList(); while(dtr.Read()) { object[] values = new object[dtr.FieldCount]; dtr.GetValues(values); al.Add(values); counter = counter++; } If I write al it returns the correct values, but I can't get theese values in to a double. How can I do this? Is it maybe easier to put the values directly from the datareader to the byte array? Thanks!

    A H 2 Replies Last reply
    0
    • _ _Searcher_

      Hi! I'm having some problems in my code. I want to get values from a datareader to a double[]. I have read the values to an array list, this is my code: int counter = 0; ArrayList al = new ArrayList(); while(dtr.Read()) { object[] values = new object[dtr.FieldCount]; dtr.GetValues(values); al.Add(values); counter = counter++; } If I write al it returns the correct values, but I can't get theese values in to a double. How can I do this? Is it maybe easier to put the values directly from the datareader to the byte array? Thanks!

      A Offline
      A Offline
      Aryadip
      wrote on last edited by
      #2

      hi, your question is not clear. I don't understand what exactly do you want... Well here are the 2 possibilities that I can think of... 1st Possiblity : If you require a specific Array of type object from the arraylist then you may use a similar code as below string[] s = new string[10]; s[0] = "Aryadip"; ArrayList al = new ArrayList(); al.Add(s); string[] ss = (string[])al[0]; MessageBox.Show( ss[0] ); 2nd Possibility : You require the full arraylist content in an array...in other words the array representation of the arraylist. Now the complexity is the arraylist elements are arrays Then the resulting array will be double dimentional... The code to retrieve the same is : string[] s = new string[10]; s[0] = "Aryadip"; ArrayList al = new ArrayList(); al.Add(s); string[][] ss = (string[][])al.ToArray(s.GetType()); MessageBox.Show( ss[0][0] ); Now tips for your code : DataReader is returning you an object array. After adding that object array to arraylist(like you did) you can get them back by using one of the methods mentioned above(replace string with object). In the Individual object arrays that you add to the arraylist... if all the values are of type double then you can safely typecast with double array. In case you are not sure or the object array contains values from different datatype then first obtain the object array by using one of the above methods and then use "Convert.ToDouble()" method to convert the specific array element to double value.Please refer to code below to get a better understanding... object[] s = new object[10]; s[0] = "Aryadip"; ArrayList al = new ArrayList(); al.Add(s); string[][] ss = (string[][])al.ToArray(new string[10].GetType()); MessageBox.Show( ss[0][0] ); Hope this helps you... if I have gone into wrong track... then sorry for that... regards, Aryadip. Cheers !! and have a Funky day !!

      _ 1 Reply Last reply
      0
      • _ _Searcher_

        Hi! I'm having some problems in my code. I want to get values from a datareader to a double[]. I have read the values to an array list, this is my code: int counter = 0; ArrayList al = new ArrayList(); while(dtr.Read()) { object[] values = new object[dtr.FieldCount]; dtr.GetValues(values); al.Add(values); counter = counter++; } If I write al it returns the correct values, but I can't get theese values in to a double. How can I do this? Is it maybe easier to put the values directly from the datareader to the byte array? Thanks!

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        If you want to read doubles from a DataReader like a SqlDataReader, then use SqlDataReader.GetDouble (these types of methods are declared on DataReader derivatives). If each field is a double, then continously read through your DataReader like you're doing, but then iterate over the field count and read those doubles into an array. You can either use an ArrayList or use a multi-dimensional or jagged array.

        Microsoft MVP, Visual C# My Articles

        1 Reply Last reply
        0
        • A Aryadip

          hi, your question is not clear. I don't understand what exactly do you want... Well here are the 2 possibilities that I can think of... 1st Possiblity : If you require a specific Array of type object from the arraylist then you may use a similar code as below string[] s = new string[10]; s[0] = "Aryadip"; ArrayList al = new ArrayList(); al.Add(s); string[] ss = (string[])al[0]; MessageBox.Show( ss[0] ); 2nd Possibility : You require the full arraylist content in an array...in other words the array representation of the arraylist. Now the complexity is the arraylist elements are arrays Then the resulting array will be double dimentional... The code to retrieve the same is : string[] s = new string[10]; s[0] = "Aryadip"; ArrayList al = new ArrayList(); al.Add(s); string[][] ss = (string[][])al.ToArray(s.GetType()); MessageBox.Show( ss[0][0] ); Now tips for your code : DataReader is returning you an object array. After adding that object array to arraylist(like you did) you can get them back by using one of the methods mentioned above(replace string with object). In the Individual object arrays that you add to the arraylist... if all the values are of type double then you can safely typecast with double array. In case you are not sure or the object array contains values from different datatype then first obtain the object array by using one of the above methods and then use "Convert.ToDouble()" method to convert the specific array element to double value.Please refer to code below to get a better understanding... object[] s = new object[10]; s[0] = "Aryadip"; ArrayList al = new ArrayList(); al.Add(s); string[][] ss = (string[][])al.ToArray(new string[10].GetType()); MessageBox.Show( ss[0][0] ); Hope this helps you... if I have gone into wrong track... then sorry for that... regards, Aryadip. Cheers !! and have a Funky day !!

          _ Offline
          _ Offline
          _Searcher_
          wrote on last edited by
          #4

          Hi I don't think that you understood me correctly, I'll try to explain it better this time. I want to get values from my db (that I have done). There are at the most 100 values, but there can also be less, but they are all in the same formate (numbers). I want to put theese number in to at double array so I can write them out in a graph. Thanks again!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups