c# - printing items in a list with a for loop -
i want build tab-delimited string of shopping cart content. cycle through list using loop items in cart meant outputted however, last item in list outputted.
public string display() { cartclass cartlist = cartclass.getcart(); string display = "" ; (int = 0; < cartlist.cartlist.count(); i++) { movies movie = cartlist.cartlist[i]; display = string.format(i + 1 + "." + "\t" + movie.moviename + "\t" + "£" + movie.moviecost.tostring()); } return display; } how can solve this?
side note: i'll use display on web page, @ point want understand why not return text items.
you see last because you're doing assignment in each loop iteration. need += instead of =.
public string display() { cartclass cartlist = cartclass.getcart(); string display = "" ; (int = 0; < cartlist.cartlist.count(); i++) { movies movie = cartlist.cartlist[i]; display += string.format(i + 1 + "." + "\t" + movie.moviename + "\t" + "£" + movie.moviecost.tostring()) + "\n"; } return display; } note better use stringbuilder building large strings.
Comments
Post a Comment