1

I have a list which contains some values, now I want to get all the data where ID is in the array with the values. The problem right now is that I only get 1 result while there are 13 integers in the array, so it doesn't loop.

This is my code: where messages is the array with integers.

 List<string> messageList = new List<string>();
 foreach (string i in messages)
 {  
     Recordset Persons = SDK.Create("R_PERSON", "", "PK_R_PERSON = "+i ,"" );
     if (Persons != null && Persons.RC > 0)
     {                   
         Persons.MoveFirst();

         do
         {
             string firstname = Persons.Fields["FIRSTNAME"].Value.ToString();
             string lastname = Persons.Fields["LASTNAME"].Value.ToString();

             personmessages.Add(firstname);
             personmessages.Add(lastname);

             PersoninboundSet.MoveNext();

         }
         while (!PersoninboundSet.EOF);
     }
     return personmessages;
 }


 messages.Add("Error, didn't work.");
 return messages;// null;

Can someone tell me what I'm doing wrong?

7
  • are you sure it is not SDKRecordset PersoninboundSet = IQSDK.CreateRecordset("R_PERSON", "", "PK_R_PERSON = "+i ,"" ); returning only one record, did step through it? Commented Jun 26, 2013 at 14:10
  • Your List contains Strings, NOT integers. Commented Jun 26, 2013 at 14:14
  • 2
    You return personmessages at the end of your first loop iteration. This means it is not possible for the loop to ever continue past the first iteration. I'm guessing this is not the behavior you're intending. Commented Jun 26, 2013 at 14:15
  • @ N4TKD Yes, that is the one that returns only 1 record. While list messages contains 13 records. So normally SDKRecordset PersoninboundSet = IQSDK.CreateRecordset("R_PERSON", "", "PK_R_PERSON = "+i ,"" ); should return 13 results too. Commented Jun 26, 2013 at 14:15
  • @DanBryant , where should I return the personmessages then? Commented Jun 26, 2013 at 14:17

3 Answers 3

6

I think you need to move the return outside the loop.

List<string> personmessages = new List<string>();
 foreach (string i in messages)
 {  
    //// Do stuff.
 }
 return personmessages;
Sign up to request clarification or add additional context in comments.

1 Comment

I already got the solution, I forgot to close the if statement of a loop above this code. But thank you guys anyway and I will accept this answer.
1

As Dan mentioned above, when you call return in a function, you'll leave the function even if it isn't finished going through a loop. If you want to have a separate return for each iteration of the loop, I'd suggest storing each result in an array, then simply return the array when the loop has finished.

List<string> personmessages = new List<string>();
List[] messageArray = new List[messages.length()];
foreach (string i in messages)
{  
    SDKRecordset PersoninboundSet = IQSDK.CreateRecordset("R_PERSON", "", "PK_R_PERSON = "+i ,"" );
 if (PersoninboundSet != null && PersoninboundSet.RecordCount > 0)
 {

     PersoninboundSet.MoveFirst();

     do
     {
         string firstname = PersoninboundSet.Fields["FIRSTNAME"].Value.ToString();
         string lastname = PersoninboundSet.Fields["LASTNAME"].Value.ToString();

         personmessages.Add(firstname);
         personmessages.Add(lastname);

         PersoninboundSet.MoveNext();

     }
     while (!PersoninboundSet.EOF);
 }
 messageArray[i] =  personmessages;
}
return messageArray;

Comments

1

It looks like you've got the order of your looping and checking mixed up. You want to return personmessages outside of the foreach loop.

Assuming you want to find each messages, and return an error if any message cannot be found, you'll want to move your error condition inside the foreach loop.

 List<string> personmessages = new List<string>();
 foreach (string i in messages)
 {  
     SDKRecordset PersoninboundSet = IQSDK.CreateRecordset("R_PERSON", "", "PK_R_PERSON = "+i ,"" );
     if (PersoninboundSet != null && PersoninboundSet.RecordCount > 0)
     {

         PersoninboundSet.MoveFirst();

         do
         {
             string firstname = PersoninboundSet.Fields["FIRSTNAME"].Value.ToString();
             string lastname = PersoninboundSet.Fields["LASTNAME"].Value.ToString();

             personmessages.Add(firstname);
             personmessages.Add(lastname);

             PersoninboundSet.MoveNext();

         }
         while (!PersoninboundSet.EOF);
     } 
     else 
     {
         // *this* i could not be found
         messages.Add("Error, didn't work.");
         return messages;// null;
     }
 }
 return personmessages; // after we've processed *all* messages

Alternatively, maybe you wanted to return an error if no message could be found. In that case, keep your error checking outside the loop, but check the Count to determine:

 List<string> personmessages = new List<string>();
 foreach (string i in messages)
 {  
     SDKRecordset PersoninboundSet = IQSDK.CreateRecordset("R_PERSON", "", "PK_R_PERSON = "+i ,"" );
     if (PersoninboundSet != null && PersoninboundSet.RecordCount > 0)
     {

         PersoninboundSet.MoveFirst();

         do
         {
             string firstname = PersoninboundSet.Fields["FIRSTNAME"].Value.ToString();
             string lastname = PersoninboundSet.Fields["LASTNAME"].Value.ToString();

             personmessages.Add(firstname);
             personmessages.Add(lastname);

             PersoninboundSet.MoveNext();

         }
         while (!PersoninboundSet.EOF);
     }
 }
 if (personmessages.Count == 0) 
 {
     // none found
     messages.Add("Error, didn't work.");
     return messages;// null;
 }      
 else
 {
     return personmessages; // after we've processed *all* messages
 }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.