Tuesday, 15 June 2010

c# - Why is my for loop skipping to the end? -


I have been asked to create a mini and basic shopping basket, this code was working but recently I Loops are created. All codes are left inside the loop to search through an array. I have provided the following code:

 For  (int i = 0; i   

I am new enough new and I may have left some silly thanks for any help

I think your code should look like this:

  bool found = false; (Art I = 0; i.e. orders.tex; i ++) {// If a match is found then it will add to the current amount if (order etams [i] .productname == product name) {order items [I] .AddItem (latest price, quantity); Found = true; }} // If no match is found in the list, then it will be added if (! Found) OrderItems.Add (new OrderItem (productName, latestValue, quantity));   

You will need a loop through an existing item. If found, update that item only after you check all the items, you can check whether you have found the item. If you have not found the item, add it as a new item.

Your original code does not work because there was no item in OrderItems , and the else statement will never be executed.

and consider the name of the AddItem instead of UpdateItem , if the method actually updates the item.

No comments:

Post a Comment