将ArrayList对象转换为double [英] convert ArrayList object to double
问题描述
在下面的代码中,我希望ArrayList对象x的类型为double。
我该怎么做?
- -Thanks
SqlConnection objConnection = new
SqlConnection(" server =(local)\\SQLEXPRESS; database = Northwind; integrated
security = true;");
String strSQL =" SELECT ProductName,UnitsInStock FROM Products
WHERE UnitsInStock> = 50" ;;
SqlCommand objCommand = new SqlCommand(strSQL,objConnection);
objConnection.Open();
ArrayList strLabel = new ArrayList();
ArrayList x = new ArrayList();
SqlDataReader dr = objCommand.ExecuteReader();
while(dr.Read())
{
object [] values1 = new object [0];
dr.GetValues (values1);
strLabel.Add(values1);
object [] values2 = new object [1];
dr.GetValues(values2);
x.Add(values2) ;
}
dr.Close();
objConnection.Close();
你需要CLR 2.0泛型...
List< double> x =新列表< double>();
Web学习者写道:在下面的代码中,我希望ArrayList对象x的类型为double。
我该怎么办?
- 谢谢
SqlConnection objConnection = new
SqlConnection(" server =(local) \\SQLEXPRESS; database = Northwind; integrated
security = true;");
String strSQL =" SELECT ProductName,UnitsInStock FROM Products
WHERE UnitsInStock> = 50" ;;
SqlCommand objCommand = new SqlCommand(strSQL,objConnection);
objConnection.Open();
ArrayList strLabel = new ArrayList();
ArrayList x = new ArrayList();
SqlDataReader dr = objCommand.ExecuteReader();
while(dr.Read())
{
对象[] values1 =新对象[0];
dr.GetValues(values1 );
strLabel.Add(values1);
object [] values2 = new object [1];
dr.GetValues(values2);
x.Add (values2);
}
dr.Close();
objConnection.Close();
我更改了代码如下。
List< string> ProductName = new List< string>();
List< double> UnitsInStock =新列表< double>();
SqlDataReader dr = objCommand.ExecuteReader();
while(dr.Read())
{
ProductName.Add(dr.GetValue(0));
UnitsInStock.Add(dr.GetValue(1));
}
现在我收到以下错误。
''System.Collections.Generic的最佳重载方法匹配。 List< string> .Add(strin g)''有一些无效的参数
最好的重载方法匹配''System.Collections.Generic.List< double> .Add(doubl) e)''有一些无效的论点
还有什么想法吗?
-
< ag ****** @ gmail.com>在留言新闻中写道:11 ********************** @ g10g2000cwb.googlegr oups.com ...你需要CLR 2.0那个泛型......
List< double> x =新列表< double>();
Web学习者写道:在下面的代码中,我希望ArrayList对象x的类型为double。
我该怎么做?
- 谢谢
SqlConnection objConnection = new
SqlConnection(" server =(local)\\ SQLEXPRESS; database = Northwind; integrated
security = true;");
String strSQL =" SELECT ProductName,UnitsInStock FROM Products
WHERE UnitsInStock> = 50";
SqlCommand objCommand = new SqlCommand(strSQL,objConnection);
objConnection.Open();
ArrayList strLabel = new ArrayList();
ArrayList x = new ArrayList ();
SqlDataReader dr = objCommand.ExecuteReader();
while(dr.Read())
{
object [] values1 =新对象[0];
dr.GetValues(values1);
strLabel.Add(values1);
object [] values2 = new object [1];
dr.GetValues(values2);
x.Add(values2);
}
dr.Close();
objConnection.Close();
< blockquote>如果您知道数据行中的第一项是字符串,
,第二项是双精度,并且它们都不能为空,那么
你可以这样做:
ProductName.Add((string)dr.GetValue(0));
UnitsInStock.Add((double) Dr.GetValue(1));
In the following code, I want ArrayList object x to be of type double.
How can I do that?
--Thanks
SqlConnection objConnection = new
SqlConnection("server=(local)\\SQLEXPRESS; database=Northwind; integrated
security=true;");
String strSQL = "SELECT ProductName, UnitsInStock FROM Products
WHERE UnitsInStock >= 50";
SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
objConnection.Open();
ArrayList strLabel = new ArrayList();
ArrayList x = new ArrayList();
SqlDataReader dr = objCommand.ExecuteReader();
while (dr.Read())
{
object[] values1 = new object[0];
dr.GetValues(values1);
strLabel.Add(values1);
object[] values2 = new object[1];
dr.GetValues(values2);
x.Add(values2);
}
dr.Close();
objConnection.Close();
You need CLR 2.0 generics for that...
List<double> x = new List<double>( );
Web learner wrote:In the following code, I want ArrayList object x to be of type double.
How can I do that?
--Thanks
SqlConnection objConnection = new
SqlConnection("server=(local)\\SQLEXPRESS; database=Northwind; integrated
security=true;");
String strSQL = "SELECT ProductName, UnitsInStock FROM Products
WHERE UnitsInStock >= 50";
SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
objConnection.Open();
ArrayList strLabel = new ArrayList();
ArrayList x = new ArrayList();
SqlDataReader dr = objCommand.ExecuteReader();
while (dr.Read())
{
object[] values1 = new object[0];
dr.GetValues(values1);
strLabel.Add(values1);
object[] values2 = new object[1];
dr.GetValues(values2);
x.Add(values2);
}
dr.Close();
objConnection.Close();
I changed the code as follows.
List<string> ProductName = new List<string>();
List<double> UnitsInStock = new List<double>();
SqlDataReader dr = objCommand.ExecuteReader();
while (dr.Read())
{
ProductName.Add(dr.GetValue(0));
UnitsInStock.Add(dr.GetValue(1));
}
Now I get the following errors.
The best overloaded method match for ''System.Collections.Generic.List<string>.Add(strin g)'' has some invalid arguments
The best overloaded method match for ''System.Collections.Generic.List<double>.Add(doubl e)'' has some invalid arguments
Any further idea, please??
--
<ag******@gmail.com> wrote in message news:11**********************@g10g2000cwb.googlegr oups.com...You need CLR 2.0 generics for that...
List<double> x = new List<double>( );
Web learner wrote:In the following code, I want ArrayList object x to be of type double.
How can I do that?
--Thanks
SqlConnection objConnection = new
SqlConnection("server=(local)\\SQLEXPRESS; database=Northwind; integrated
security=true;");
String strSQL = "SELECT ProductName, UnitsInStock FROM Products
WHERE UnitsInStock >= 50";
SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
objConnection.Open();
ArrayList strLabel = new ArrayList();
ArrayList x = new ArrayList();
SqlDataReader dr = objCommand.ExecuteReader();
while (dr.Read())
{
object[] values1 = new object[0];
dr.GetValues(values1);
strLabel.Add(values1);
object[] values2 = new object[1];
dr.GetValues(values2);
x.Add(values2);
}
dr.Close();
objConnection.Close();
If you know for a fact that the first item in the data row is a string,
and the second is a double, and neither of them can ever be null, then
you can do this:
ProductName.Add((string)dr.GetValue(0));
UnitsInStock.Add((double)dr.GetValue(1));
这篇关于将ArrayList对象转换为double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!