Solved

IEnumerable bug

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now