Reputation: 176
I want to build tab-delimited string of shopping cart content. I cycle through a list using a for loop so that items in the cart are meant to be outputted however, only the last item in that list is outputted.
public string Display()
{
CartClass CartList = CartClass.GetCart();
String display = "" ;
for (int i = 0; i < 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 I solve this?
Side note: ultimately I'll use it to display on web page, but at this point I want to understand why it does not return text for all items.
Upvotes: 0
Views: 912
Reputation: 17064
You only see the last because you're doing assignment in each loop iteration. You need to do +=
instead of =
.
public string Display()
{
CartClass CartList = CartClass.GetCart();
String display = "" ;
for (int i = 0; i < 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 that it is generally better to use StringBuilder
for building large strings.
Upvotes: 12
Reputation: 26219
As already other answers pointed out that you need to append the String using Concatnation operator +
,I Would like to suggest you to use StringBuilder
.
StringBuilder sb = new StringBuilder();
for (int i = 0; i < CartList.CartList.Count(); i++)
{
Movies Movie = CartList.CartList[i];
sb .Append(String.Format(i + 1 + "." + "\t" + Movie.MovieName + "\t" + "£" + Movie.MovieCost.ToString());
}
return sb.ToString();
Upvotes: 3
Reputation: 152644
You are overwriting display
in each loop.
The simplest change is to change =
to +=
:
// V-----
display += String.Format(i + 1 + "." + "\t" + Movie.MovieName + "\t" + "£" + Movie.MovieCost.ToString());
But you may find StringBuilder
more performant if you have a LOT of strings.
Upvotes: 2
Reputation: 61529
I think you want this.
public string Display()
{
CartClass CartList = CartClass.GetCart();
String display = "" ;
for (int i = 0; i < CartList.CartList.Count(); i++)
{
Movies Movie = CartList.CartList[i];
display += String.Format(i + 1 + "." + "\t" + Movie.MovieName + "\t" + "£" + Movie.MovieCost.ToString());
}
return display;
}
Upvotes: 1
Reputation: 3446
You need to concatenate each entry to display as follows (notice +=
):
public string Display()
{
CartClass CartList = CartClass.GetCart();
String display = "" ;
for (int i = 0; i < CartList.CartList.Count(); i++)
{
Movies Movie = CartList.CartList[i];
display += String.Format(i + 1 + "." + "\t" + Movie.MovieName + "\t" + "£" + Movie.MovieCost.ToString());
}
return display;
}
Upvotes: 2
Reputation: 1608
You are using = sign to set Display text you need to use += or the string builder class.
Upvotes: 3
Reputation: 327
You are replacing the value of your variable with the next item in the loop... try adding it.
display += String.Format(i + 1 + "." + "\t" + Movie.MovieName + "\t" + "£" + Movie.MovieCost.ToString());
Upvotes: 2
Reputation: 12857
You really should be using a StringBuilder
here:
System.Text.StringBuilder sb = new StringBuilder();
for (int i = 0; i < CartList.CartList.Count(); i++)
{
Movies Movie = CartList.CartList[i];
sb.Append( String.Format(i + 1 + "." + "\t" + Movie.MovieName + "\t" + "£" + Movie.MovieCost.ToString()));
}
return sb.ToString();
Upvotes: 7