1

I am doing a message loop for a skype bot, it would means I can advertise/mass message easily. However, after the first name string in the loop get replaced by the name in the class, it will only use that name and not replace the new name on the next loop.

Code:

if(listView2.SelectedItems.Count<=0)
    return;

string value="Hi %name%!";
string body="";

if(Program.InputBox("New Message", "Body of the message:", ref value)==DialogResult.OK) {
    body=value;
}

foreach(ListViewItem i in listView2.SelectedItems) {
    String rawID=i.SubItems[7].ToString();
    String[] splitID=rawID.Split('}');
    String[] ID=splitID[0].Split(new char[] { '{' });

    body=body.Replace("%handle%", SkypeUser.Users[Convert.ToInt32(ID[1])].Handle);
    body=body.Replace("%name%", SkypeUser.Users[Convert.ToInt32(ID[1])].Name);
    body=body.Replace("%status%", SkypeUser.Users[Convert.ToInt32(ID[1])].Status);
    body=body.Replace("%country%", SkypeUser.Users[Convert.ToInt32(ID[1])].Country);
    body=body.Replace("%mood%", SkypeUser.Users[Convert.ToInt32(ID[1])].Mood);
    body=body.Replace("%about%", SkypeUser.Users[Convert.ToInt32(ID[1])].About);

    if(!(body==String.Empty)) {
        skype.SendMessage(SkypeUser.Users[Convert.ToInt32(ID[1])].Handle, body);
    }
}

If I select three people, named Jim, Tim and Derp, then the first person on the selected list is Jim. If I used %name%, it will be correctly replaced to "Jim" in the message to Jim, but the messages to Tim and Derp will also have "Jim", instead of the string replacing %name% to their names.


Edit:

I know putting value, body and if-statements inside the loop; but I want that requiring to input message would be only once. That's the whole point of the mass message.

1
  • In that case you can use the temporary variable which will get assigned each time with body.. There is one more solution to it but it it will require bit more efforts. you can keep the code as it is and can use RegularExpression.replace() and take the replaced value in another string instead of taking it in variable "body"... Commented Feb 25, 2013 at 12:26

5 Answers 5

2

following statement should be inside the for loop.

            string value = "Hi %name%!";
            string body = "";
            if (Program.InputBox("New Message", "Body of the message:", ref value) == DialogResult.OK)
            {
                body = value;
            }
Sign up to request clarification or add additional context in comments.

2 Comments

Read my edit.I know that is a fixed, but its not a fix for my solution.
yeah i've added a comment to your solution after you edited that :) happy coding
2

First time you go through the loop, the %% placeholders are effectively destroyed. If you go in with:

body = "Hi %name%";

after the first iteration you get:

body = "Hi Jim";

When the loop runs for the second time it searches for %name% 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:

foreach (ListViewItem i in listView2.SelectedItems)
{
   string userBody = body;
   ...
   userBody = userBody.Replace("%name%", SkypeUser.Users[Convert.ToInt32(ID[1])].Name);
   ...
}

Also note that the string 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 StringBuilder class, that is designed to used in string manipulation. Your code will look somewhat like:

foreach (ListViewItem i in listView2.SelectedItems)
{
   StringBuilder userBodyBuilder = new StringBuilder(body);
   ...
   userBodyBuilder.Replace("%name%", SkypeUser.Users[Convert.ToInt32(ID[1])].Name);
   ...
}

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

Comments

0

I think that the first time you replace your data %XXX% ("%name%" for example), the next time you pass in the loop you don't have them anymore.

Use a new variable in your loop :

foreach[...]
{
  string currentBody = body;
  currentBody = body.Replace[...]
  [...]
}

Comments

0

Just replace the string again after sending the message<3

Easy code for you to copy

if(SkypeUser.Users[Convert.ToInt32(ID[1])].Handle!=String.Empty) {
    body=body.Replace(SkypeUser.Users[Convert.ToInt32(ID[1])].Handle, "%handle%");
}

if(SkypeUser.Users[Convert.ToInt32(ID[1])].Name!=String.Empty) {
    body=body.Replace(SkypeUser.Users[Convert.ToInt32(ID[1])].Name, "%name%");
}

if(SkypeUser.Users[Convert.ToInt32(ID[1])].Status!=String.Empty) {
    body=body.Replace(SkypeUser.Users[Convert.ToInt32(ID[1])].Status, "%status%");
}

if(SkypeUser.Users[Convert.ToInt32(ID[1])].Country!=String.Empty) {
    body=body.Replace(SkypeUser.Users[Convert.ToInt32(ID[1])].Country, "%country%");
}

if(SkypeUser.Users[Convert.ToInt32(ID[1])].Mood!=String.Empty) {
    body=body.Replace(SkypeUser.Users[Convert.ToInt32(ID[1])].Mood, "%mood%");
}

if(SkypeUser.Users[Convert.ToInt32(ID[1])].About!=String.Empty) {
    body=body.Replace(SkypeUser.Users[Convert.ToInt32(ID[1])].About, "%about%");
}

Comments

-1

That's because you are replacing the string body in your loop. Create a temp variable in the loop and assign to that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.