2013-06-25 54 views
1

我是C#的新手,现在我完全陷入这个函数。 任何帮助,将不胜感激。C#OutOfMemoryException数组

我得到一个OutOfMemoryException mess.Add(firstname); 我敢肯定,这是因为数组故障,但我似乎无法使其工作。

谁能以正确的方式引导我?

这是我到目前为止的代码:

public List<string> SelectEmployee(string pkrelation) 
     { 
     SDKRecordset inboundSet = IQSDK.CreateRecordset("R_CONTACT", "", "FK_RELATION = " + pkrelation, ""); 
     inboundSet.MoveFirst(); 
     string person = inboundSet.Fields["FK_PERSON"].Value.ToString(); 
     messages.Add(person); 

     inboundSet.MoveNext(); 
     SDKRecordset inboundSet2 = IQSDK.CreateRecordset("R_PERSON", "", "PK_R_PERSON = " + person, ""); 


     if (inboundSet2 != null && inboundSet2.RecordCount > 0) 
     { 
      inboundSet2.MoveFirst();    

      do 
      { 
       string firstname = inboundSet2.Fields["FIRSTNAME"].Value.ToString(); 
       mess.Add(firstname); 

       inboundSet.MoveNext(); 
      } 
      while (!inboundSet2.EOF); 
      return mess; 


     } 

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

出于好奇,“inboundSet2”中有多少条记录?你确定'EOF'在经过合理次数的迭代之后被击中了吗?编辑:你有一个错字吗?不应该是'inboundSet2.MoveNext()',而不是'inboundSet.MoveNext()'? –

+0

在这种情况下,看看它是否适用于非常小的记录集,如10和10.如果出现错误* still *,则清楚地处理了从不退出或类似的循环。 #basictroubleshooting – catfood

+1

什么是'混乱'?和'messages'一样吗?你在某处清理它吗? – JeffRSon

回答

8

你有一个错字。你不小心有inboundSet.MoveNext()所以自然你的inboundSet2.EOF永远不会设置为false,因为你从来没有真正遍历它。这会导致最终达到OutOfMemoryException的无限循环。

do 
{ 
    string firstname = inboundSet2.Fields["FIRSTNAME"].Value.ToString(); 
    mess.Add(firstname); 

    inboundSet.MoveNext(); // This needs to be inboundSet2! 
} 
while(!inboundSet2.EOF) //EOF never becomes true causing an infinite loop 
+0

谢谢你们!这是问题,我猜我需要更多的咖啡! – Matheno

+1

@Marijke我还可以提出一些更好的变量命名和咖啡。例如'contactInboundSet'和'personInboundSet',而不是'inboundSet'和'inboundSet2' :)本来有立即明显的东西是错误的(比没有更可能_永远_写/使用摆在首位的一系列错误) –

+0

感谢您的反馈,这是一个完美的想法! – Matheno