Converting an array list to double array
-
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!
-
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!
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 !!
-
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!
If you want to read doubles from a
DataReader
like aSqlDataReader
, then useSqlDataReader.GetDouble
(these types of methods are declared onDataReader
derivatives). If each field is a double, then continously read through yourDataReader
like you're doing, but then iterate over the field count and read those doubles into an array. You can either use anArrayList
or use a multi-dimensional or jagged array.Microsoft MVP, Visual C# My Articles
-
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 !!
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!