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

Popular posts from this blog

Why can rails not find a route created by a helper? -

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -