0

I need to add decimal numbers held in the last index of arraylists, to a decimal subtotal, for Windows Forms. My processOrder MenuItem gives error:

subList1 does not exist in the current context.

But subLists are ok in my AddtoCart MenuItem method when using .Add. If I enter subtotal = (subList1.RemoveAt(3) + subList2.RemoveAt(3)); in AddtoCart() as a test the error is:

cannot use + operator with operands of type void and void.

I do not want to use another class and getters. Doesn't matter if values are removed, I just need to obtain the values and add them for my solution to work.

3
  • 1
    You should really start formatting your question better. Also, try to have a little bit more logic on what you are asking. I for one, haven't understood a thing Commented Jan 23, 2015 at 23:28
  • Could you show the code generating the array list? Also, since .NET 2 ArrayList is deprecated. Use List<T> instead (yes, that requires a class. C# is OOP). Commented Jan 23, 2015 at 23:31
  • BTW RemoveAt is for removing and not reading values. Commented Jan 24, 2015 at 2:20

3 Answers 3

3

The RemoveAt() function doesn't return the value. It just removes it and throws it away. Instead, this would work:

subtotal = ((decimal)subList1[subList1.Count-1]) + (decimal)subList2.[subList2.Count - 1];

Even better, you can get rid of that ugly casting if you use a simple List<decimal> instead of an ArrayList. There's really no good reason to ever use an ArrayList anymore.

subtotal = subList1[subList.Count - 1] + subList2[subList2.Count - 1];

If you're using a recent version of Visual Studio you can simplify the code further:

subtotal = subList1.Last() + subList2.Last();
Sign up to request clarification or add additional context in comments.

Comments

0

You should definitely practice some more C# and read about collections. You're not looking for RemoveAt() as it simply removes an item from a collection (ArrayList in this case), and is a void method, which has no return value.

What you need is subtotal = subList1[3] + subList2[3].

1 Comment

Let me re-state: I'm working in Windows Forms. I'm trying to add the numbers from the last element of my arraylists. nXu corrected my syntax but subtotal = ((decimal)subList1[2] + subList2[2] .... (decimal)subList5[2]); generates error 'Argument out of range exception'.
0

Let me re-state for barca_d: I'm working in Windows Forms. I'm trying to add the numbers from the last element of my arraylists. nXu corrected my syntax but subtotal = ((decimal)subList1[2] + subList2[2] .... (decimal)subList5[2]); generates error 'Argument out of range exception'. From what I understand, before input, the index value is -1, afterwards value must be 0-2.

From what I can find, List can only use one type. Hence, using arraylist. Teacher has only gone over arrays but says arraylists are better. We haven't gone over hashtable and other collections yet. Arraylists were generated like this: private ArrayList subList1 = new ArrayList(3);

Can List<> use multiple types somehow? My counter for AddtoCart Menu Item must not be working. Solution was to initialize arraylist elements to null.

Thank you for the feedback & tips.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.