Solved

IEnumerable bug

Posted on 2015-02-05
4
89 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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