C# help - arraylist stuff

VeeDubbs

Limp Gawd
Joined
Dec 9, 2005
Messages
398
Hello -

Total newbie here. I've googled until I've gone crazy and can't find what I need.

I have two arraylists. One called productList and one called priceList. They are both in a loop and I want to be able to multiply them every time the loop goes through. This isn't the exact code - but it gets my point across (these two arrays will always have the exact same amount of items in them):

Code:
for (int i = 0; i, productList.Count; i++)
{
productList[i] * priceList[i]
}

But, whenever I try to do this I get:

Specified cast is not valid.
or
When casting from a number, the values must be a number less than infinity.

I get this even when I try casting the lists. I hope this makes sense. I'm a total newb and way confused. Any help is appreciated!!
 
Well, there are two problems listed:

1) Your for loop isn't structured properly. I'm guessing you meant to use the less-than sign '<' rather than a comma ','.
2) You don't have an assignment operator anywhere inside the for loop. You just wrote an r-value and didn't assign it to anything or use it in a method call.

I'd strongly suggest researching either the assignment operator '=' or researching a method call that will allow you to assign the value as needed.

202276
 
One of the error messages you quote is because you can't multiply two objects.

C# didn't originally have type-safe container classes. ArrayList takes whatever you put in it and downcasts it to an object. In addition to the problems Tytalus points out, you'll need to cast the object you get from the ArrayList back to an integer before using it as an integer.

Code:
int nProduct = (int) productList[i] * (int) priceList[i];
 
To elaborate a bit more on what mike said, you might want to consider using Generic Lists (System.Collections.Generic) instead of ArrayLists if you are storing a homogenous collection of objects and using .NET 2.0
 
Thanks for all the replies!

Tytalus - you are right, I did mean to use a < instead of a comma.

Mike - I actually did try casting the lists and I did it agian after reading your message just to make sure, but I still get the exact same two error messages. I've tried them as Ints, Doubles, and Floats. So, that kind of confused, because I am casting them, so I'm not sure about that first error.

Fryguy - I noticed that I do have Systems.Collections already being used. You think using Systems.Collections.Generic will help me? Should I use both or just one or the other?

Thanks again guys!!
 
Do you receive those errors at runtime, or at compile time?
 
IMO using a List<Int32> is your best bet in this situation. Generic Lists are simple to utilize and support what you want to do in this situation.

The System.Collections.Generic namespace: Link
As a quickie example, if you generated a List<Widget> and a List<Box>, and neither Widget nor Box were derived from one another, then attempting to call the Add method of List<Widget> would give you a compiler error if you provided a Box as an argument, and vise-versa. Type-safe collections are yet another way to ensure that your code will run as expected. :)

The List<T> Class: http://msdn2.microsoft.com/en-us/library/6sh2ey19.aspx

202276
 
That suggests you actually do have a heterogeneous collection; when you pull those values, they're not actually integers.
 
I'm glad to say - I finally got it to work. Instead of casting I had to convert the items in the arraylists - such as:

Code:
double price = Convert.ToDouble(priceList[i]);
double quantity = Convert.ToDouble(quantList[i]);

Thanks for all the replies everyone!
 
show us the declarations for priceList/productList. while Convert.ToDouble() may get the job done it might not be the most efficient way to do it.
 
show us the declarations for priceList/productList. while Convert.ToDouble() may get the job done it might not be the most efficient way to do it.

Code:
ArrayList priceList = new ArrayList();
ArrayList productList = new ArrayList();

:p
 
What's interesting is how you put the data into those arrays, not just their declarations. If you'd rather not investigate how you might make your code better, though, just let us know so we won't waste anymore time trying to help you.
 
I agree with the above poster, use generic lists whenever possible. I hardly ever use an arraylist like you are trying to do.
 
Back
Top