473,803 Members | 3,758 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
14 10603
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
On 20 Mar 2006 19:30:28 -0800, "Bruce Wood" <br*******@cana da.com> wrote:
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));

or:
// no casting required
ProductName.Add (dr.GetString(0 ));
UnitsInStock.Ad d(dr.GetDouble( 1));

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Mar 21 '06 #5
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
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 #7
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 #8
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 #9
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,
6
5211
by: mm | last post by:
Hi All, I am a newbie in Vb.net and I have some problems I have a structure called padrecord. In this structure i have X,Y len h w name typeval First I add the known values in X and Y. And put then this structure in a Arraylist Afterwards I want to add the values of len h name and typeval to all the padrecords in the arraylist The count of items in the arraylist can be between 1 and 5000
15
1714
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...
11
1887
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
4
1250
by: vivekian | last post by:
Hi, There is an ArrayList AppList which contains objects of the type Appliance. The type Appliance consists of a string description and a double price. In what way can an object which has been added to the ShoppingCart be removed , by only knowing the string description. public class Appliance:IAppliance { string description ;
3
8753
by: mrajanikrishna | last post by:
Hi Friends, I am accepting a number from the user entered in a textbox. I want to assign to a variable in my code and assignt this to that variable. double num1 = (double)txtNum1.text; this produced an error
0
1351
by: batbrandman | last post by:
Need help with this...I tihnk it would be easier to see what someones developed program looks like, I keep getting stuck on a few major parts...Thanks very much!!!!!!!!!! The data will be found in a student.txt file . A sample is shown below. I also want to implement the string tokenizer Clark Kent,190409211,1209 Lynn Lane,Burton,OH,21091 Lois Lane,524673319,57 Wilson Avenue,Wheeling,WVA,35120 Carl Sampson,451239813,401 Mercer...
5
3012
by: blt51 | last post by:
I need to write a program that handles a bank account and does a number of transactions using an arraylist. However, I'm having trouble getting the arraylist to store all the transactions and then output them. There are 2 classes, Bank and BankAccount. I also have a tester. Any help would be appreciated. Bank package bank; import java.util.ArrayList; /** This bank contains a collection of bank accounts.
0
9564
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10546
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
10310
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...
0
9121
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
7603
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
6841
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
5498
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...
0
5627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.