Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First time you go through the loop, the <code>%%</code> placeholders are effectively destroyed. If you go in with:</p> <pre><code>body = "Hi %name%"; </code></pre> <p>after the first iteration you get:</p> <pre><code>body = "Hi Jim"; </code></pre> <p>When the loop runs for the second time it searches for <code>%name%</code> in "Hi Jim", does not find anything to replace, leaves the string alone, and you end up sending "Hi Jim" to Derp. Avoid modifying the original value of the body, and use a fresh variable in each iteration:</p> <pre><code>foreach (ListViewItem i in listView2.SelectedItems) { string userBody = body; ... userBody = userBody.Replace("%name%", SkypeUser.Users[Convert.ToInt32(ID[1])].Name); ... } </code></pre> <p>Also note that the <code>string</code> class in C# is immutable, which means that each string operation in the loop creates a new instance of a string with modified contents, and leaves the previous value as garbage to be collected. If you do significant string manipulation (and you do) look into the <a href="http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx" rel="nofollow"><code>StringBuilder</code></a> class, that is designed to used in string manipulation. Your code will look somewhat like:</p> <pre><code>foreach (ListViewItem i in listView2.SelectedItems) { StringBuilder userBodyBuilder = new StringBuilder(body); ... userBodyBuilder.Replace("%name%", SkypeUser.Users[Convert.ToInt32(ID[1])].Name); ... } </code></pre> <p>This code will waste significantly less memory and be significantly faster than your original code, so you'll be able to spam your contacts more efficiently :D</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload