Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

IEnumerable bug

Posted on 2015-02-05
4
90 Views
Last Modified: 2015-02-07
Hello,
I am having some problem with IEnumberable<string>
I get this error in the function Send.
Additional information: ERROR: 306 Incorrect msisdn parameter in request

string myText = "Some text";
string idString = "27";
SMSSender mySender = new SMSSender("mmmmm", "nnnnn");
IEnumerable<string> m_oEnum = new List<string>(){ idString };
mySender.Send(m_oEnum, myText);

 public void Send(IEnumerable<string> recipients, string text)
        {
            const int chunkSize = 255;
            while (recipients.Any())
            {
                var chunk = recipients.Take(chunkSize);
                recipients = recipients.Skip(chunkSize);
                var client = new HttpClient();
                var content = new FormUrlEncodedContent(new[] {
                    new KeyValuePair<string, string>("L", this.username),
                    new KeyValuePair<string, string>("P", this.password),
                    new KeyValuePair<string, string>("MSISDN", string.Join(",", chunk)),
                    new KeyValuePair<string, string>("T", text)
                });
                var response = client.PostAsync(this.uri, content).Result;
                response.EnsureSuccessStatusCode();
                var body = response.Content.ReadAsStringAsync().Result;
                if (body.StartsWith("ERROR")) throw new Exception(body);
            }
        }


If I write the number directly in like this it works.
IEnumerable<string> m_oEnum = new List<string>(){ "27"};

Do you know what I am doing wrong?
0
Comment
Question by:johnson1
  • 2
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40592665
This isn't so much an answer to your problem as it is a suggestion, but you've got an endless loop. Calling Take does not actually remove elements from an enumerable--truth be told, you can't remove elements from an enumerable. So your call to Any will always return true, assuming there is at least one element in the enumerable. You'll want to change the condition of your loop such that you have a valid exit condition.
0
 

Author Comment

by:johnson1
ID: 40593605
If I do it like this it works:

SMSSender mySender = new SMSSender("mmmmm", "nnnnn");
IEnumerable<string> m_oEnum = new List<string>(){ "27"};

but it does not work if I do it like this

string idString = "27";
SMSSender mySender = new SMSSender("mmmmm", "nnnnn");
IEnumerable<string> m_oEnum = new List<string>(){ idString};

So the question is why can I not use idString instead of "27"
0
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 40594707
What VS/.NET version are you using?
The code below seems to work OK in VS 2013 (.NET 4.5.1)
        public static string GetDemo()
        {
            string data = "27";
            IEnumerable<string> m_oEnum = new List<string>() { data };
            var chunk = m_oEnum.Take(255);
            return string.Join(",", chunk);
        }

Open in new window

It should be easier for you if you use the constructor instead of the collection initializer:
IEnumerable<string> m_oEnum = new List<string>(new string[] { data });

Open in new window

0
 

Author Closing Comment

by:johnson1
ID: 40595336
Thank you. By using the constructor then it worked.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question