473,831 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert ArrayList object to double

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=Northw ind; integrated
security=true;" );
String strSQL = "SELECT ProductName, UnitsInStock FROM Products
WHERE UnitsInStock >= 50";
SqlCommand objCommand = new SqlCommand(strS QL, objConnection);

objConnection.O pen();

ArrayList strLabel = new ArrayList();
ArrayList x = new ArrayList();

SqlDataReader dr = objCommand.Exec uteReader();

while (dr.Read())
{
object[] values1 = new object[0];
dr.GetValues(va lues1);
strLabel.Add(va lues1);

object[] values2 = new object[1];
dr.GetValues(va lues2);
x.Add(values2);
}

dr.Close();
objConnection.C lose();
Mar 21 '06 #1
11 1887
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=Northw ind; integrated
security=true;" );
String strSQL = "SELECT ProductName, UnitsInStock FROM Products
WHERE UnitsInStock >= 50";
SqlCommand objCommand = new SqlCommand(strS QL, objConnection);

objConnection.O pen();

ArrayList strLabel = new ArrayList();
ArrayList x = new ArrayList();

SqlDataReader dr = objCommand.Exec uteReader();

while (dr.Read())
{
object[] values1 = new object[0];
dr.GetValues(va lues1);
strLabel.Add(va lues1);

object[] values2 = new object[1];
dr.GetValues(va lues2);
x.Add(values2);
}

dr.Close();
objConnection.C lose();


Mar 21 '06 #2
I changed the code as follows.

List<string> ProductName = new List<string>();
List<double> UnitsInStock = new List<double>();

SqlDataReader dr = objCommand.Exec uteReader();
while (dr.Read())
{
ProductName.Add (dr.GetValue(0) );
UnitsInStock.Ad d(dr.GetValue(1 ));
}

Now I get the following errors.

The best overloaded method match for 'System.Collect ions.Generic.Li st<string>.Add( string)' has some invalid arguments

The best overloaded method match for 'System.Collect ions.Generic.Li st<double>.Add( double)' has some invalid arguments

Any further idea, please??

--


<ag******@gmail .com> wrote in message news:11******** **************@ g10g2000cwb.goo glegroups.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=Northw ind; integrated
security=true;" );
String strSQL = "SELECT ProductName, UnitsInStock FROM Products
WHERE UnitsInStock >= 50";
SqlCommand objCommand = new SqlCommand(strS QL, objConnection);

objConnection.O pen();

ArrayList strLabel = new ArrayList();
ArrayList x = new ArrayList();

SqlDataReader dr = objCommand.Exec uteReader();

while (dr.Read())
{
object[] values1 = new object[0];
dr.GetValues(va lues1);
strLabel.Add(va lues1);

object[] values2 = new object[1];
dr.GetValues(va lues2);
x.Add(values2);
}

dr.Close();
objConnection.C lose();

Mar 21 '06 #3
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.Get Value(0));
UnitsInStock.Ad d((double)dr.Ge tValue(1));

Mar 21 '06 #4
The Add() methods exepct a string and double, respectively. Try this:

ProductName.Add (dr.GetString(0 ));
UnitsInStock.Ad d(dr.GetDouble( 1));

or this:

ProductName.Add ((string)dr.Get Value(0));
UnitsInStock.Ad d((double)dr.Ge tValue(1));

-Carl

Mar 21 '06 #5
Will the resulting array be in following form?

double[] UnitsInStock = { 120, 104, 112, 111 };
string[] ProductName = { "Boysenberr y", "Broed", "Momo", "Taifuk" };
--The
"carl" <cz******@gmail .com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
The Add() methods exepct a string and double, respectively. Try this:

ProductName.Add (dr.GetString(0 ));
UnitsInStock.Ad d(dr.GetDouble( 1));

or this:

ProductName.Add ((string)dr.Get Value(0));
UnitsInStock.Ad d((double)dr.Ge tValue(1));

-Carl

Mar 21 '06 #6
For ProductName, both of the suggested methods works.

But with
UnitsInStock.Ad d(objReader.Get Double(1));
or
UnitsInStock.Ad d((double)objRe ader.GetValue(1 ));

I get the following error:

Exception Details: System.InvalidC astException: Specified cast is not valid.

"carl" <cz******@gmail .com> wrote in message news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
The Add() methods exepct a string and double, respectively. Try this:

ProductName.Add (dr.GetString(0 ));
UnitsInStock.Ad d(dr.GetDouble( 1));

or this:

ProductName.Add ((string)dr.Get Value(0));
UnitsInStock.Ad d((double)dr.Ge tValue(1));

-Carl

Mar 21 '06 #7
Web learner <be******@learn ing.edu> wrote:
For ProductName, both of the suggested methods works.

But with
UnitsInStock.Ad d(objReader.Get Double(1));
or
UnitsInStock.Ad d((double)objRe ader.GetValue(1 ));

I get the following error:

Exception Details: System.InvalidC astException: Specified cast is not valid.


Well, that suggests that column 1 isn't actually a double.

I suggest you fetch it as an object, then see what type it actually is.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 21 '06 #8
cast the value to the type
ProductName.Add ((string)dr.Get Value(0));

Also, you will want to follow the .NET framework coding rules. I was
rather confused at what you were doing. PascalCasing is for
Properties, MethodNames, and Types. You should make your variables
camelCased as...

List<string> productName = new List<string>();
List<double> unitsInStock = new List<double>();

this will help remove confusion in the future.

Here's a link for the guidelines...
http://msdn.microsoft.com/library/de...guidelines.asp

You will definately want to buy the book....
http://www.amazon.com/gp/product/032...lance&n=283155
the most important book you will ever buy for .NET or any development.

Let me know if you have other questions...

Mar 21 '06 #9
Thanks for your helps. I have modified the code and attach it at the bottom of this message.

At line unitsInStock.Ad d((double)dr.Ge tValue(1)); I still get same error: Specified cast is not valid

If you comment out above code line and also Response.Write( unitsInStock[0]);
then the code works fine. That means it works for productName array.

My purpose is to populate an array from SqlDataReader as below:

double[] unitsInStock = { 120, 104, 112, 111 };
string[] productName = { "Boysenberr y", "Something" , "Another item", "Demo item" };
--------------------------------------------

<%@ Import Namespace="Syst em.Data" %>
<%@ Import Namespace="Syst em.Data.SqlClie nt" %>
<%@ Import Namespace="Syst em.Web.UI" %>
<%@ Import Namespace="Syst em.Collections. Generic" %>
<script language="c#" runat="server">
private void Page_Load(objec t sender, System.EventArg s e)
{
SqlConnection objConnection = new SqlConnection(" server=(local)\ \SQLEXPRESS; database=Northw ind; integrated security=true;" );
String strSQL = "SELECT productName, unitsInStock FROM Products WHERE unitsInStock >= 100";
SqlCommand objCommand = new SqlCommand(strS QL, objConnection);
objConnection.O pen();

SqlDataReader dr = objCommand.Exec uteReader();

List<string> productName = new List<string>();
List<double> unitsInStock = new List<double>();

while (dr.Read())
{
productName.Add ((string)dr.Get Value(0)); //No Error
unitsInStock.Ad d((double)dr.Ge tValue(1)); //ERROR : Specified cast is not valid
}
dr.Close();
objConnection.C lose();

Response.Write( productName[0]);
Response.Write( unitsInStock[0]);
}
</script>

<ag******@gmail .com> wrote in message news:11******** **************@ z34g2000cwc.goo glegroups.com.. .
cast the value to the type
ProductName.Add ((string)dr.Get Value(0));

Also, you will want to follow the .NET framework coding rules. I was
rather confused at what you were doing. PascalCasing is for
Properties, MethodNames, and Types. You should make your variables
camelCased as...

List<string> productName = new List<string>();
List<double> unitsInStock = new List<double>();

this will help remove confusion in the future.

Here's a link for the guidelines...
http://msdn.microsoft.com/library/de...guidelines.asp

You will definately want to buy the book....
http://www.amazon.com/gp/product/032...lance&n=283155
the most important book you will ever buy for .NET or any development.

Let me know if you have other questions...

Mar 21 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
3259
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point storage and dynamically resize it when there is a new point or should I use ArrayList? Is speed noticable between the two? Regards,
15
1715
by: GTi | last post by:
If I use: ArrayList TimeScale = new ArrayList(); TimeScale.Capacity = 1000; TimeScale="test 1" The last line trow me an error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
1
1235
by: Marcus Kwok | last post by:
I wrote a little test program to demonstrate what may be a misunderstanding on my part in the behavior of ArrayList::Sort(). I defined a class (Test) that has two data members: a System::String* (name) and a double value. Test implements IComparable, so I made the CompareTo() function sort first by name, then by the value of the double. When I run the code below, it sorts by name correctly, but it does not sort correctly the items...
14
10604
by: Web learner | last post by:
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
0
9793
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10777
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10494
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10534
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9317
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7748
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5620
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3964
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.