2013-06-25 114 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