2011-10-24 54 views
-1

我正在獲取indexoutofrangeexception(請參閱---->指針,用於在代碼中向下生成錯誤的指針)。該程序通過數據集表中的標題和行項目記錄進行循環。這些表格有關係。我的示例數據有2個標題,每個標題有2行。該程序有兩個循環,第一個循環遍歷標題記錄,第二個循環遍歷標題的子記錄。該計劃的c#數組 - 索引超出範圍異常

部分:

 // ***** PO Header and Line 

     int ln; 
     ln = 0; 

     // Create an eConnect PO Header node object 
     taGLTransactionHeaderInsert jeh = new taGLTransactionHeaderInsert(); 

     // Create an array for lineitems 
     taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert[] lineitems = new taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert[ln]; 

     foreach (DataRow dtrHDR in ds.Tables["Header"].Rows) 

     { 
      Array.Clear(lineitems, 0, ln); 

      jeh.BACHNUMB = "Sheraz"; 
      jeh.JRNENTRY = jenoint; 
      jeh.REFRENCE = dtrHDR["Reference"].ToString(); 
      jeh.SOURCDOC = dtrHDR["AvantisJE"].ToString(); 
      jeh.USERID = System.Environment.UserName; 
      jeh.TRXDATE = System.DateTime.Now.ToString(); 

      ln = 0; 

      foreach (DataRow dtrLine in dtrHDR.GetChildRows("HdrLine")) 

      { 

       // Populate the elements of the taPoLIne_ItemsTaPOLine XML node 
       taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert jel = new taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert(); 

       jel.BACHNUMB = jeh.BACHNUMB; 
       jel.JRNENTRY = jeh.JRNENTRY; 
       jel.ACTNUMST = dtrLine["GreatPlains"].ToString(); 
       jel.DEBITAMT = Convert.ToDecimal(dtrLine["Debit"].ToString()); 

       //Avantis Inv Trx Key 
       jel.ORDOCNUM = dtrLine["AvantisJE_Line"].ToString(); 
       // Avantis GL Trx Type 
       jel.ORTRXDESC = dtrLine["transactiontypename"].ToString(); 


       //Add POLine to an Array 
       lineitems[ln] = jel;  ----------------> I get an error here! 

       ln = ln + 1; 

       Array.Resize(ref lineitems, ln + 1); 

      } 

     } 
+0

你是如何初始化數組 – Sandy

+0

taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert [] =了LineItem新taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert [LN]; – Shazam

回答

2

您正在訪問尚不存在的索引。

//Add POLine to an Array 
lineitems[ln] = jel;  ----------------> I get an error here! 
ln = ln + 1;  
Array.Resize(ref lineitems, ln + 1); 

您需要的順序更改爲:

//Add POLine to an Array 
Array.Resize(ref lineitems, ln + 1); 
lineitems[ln] = jel;  ----------------> should be fixed, no error here! 
ln = ln + 1;  

編輯:現在,眼前的問題是出的方式,到一個更好的實施。

數組的大小是固定的,調整數組的大小是一個昂貴的操作(基本上它需要創建一個新大小的副本)。通常你會在識別性能瓶頸後使用這些。在大多數情況下,使用列表會更好。

我建議修改這一行:

// Create an array for lineitems 
taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert[] lineitems = 
       new taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert[ln]; 

到:

var lineitems = new List<taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert>(); 

,然後添加到它,你只需做

lineitems.Add(jel); 

遍歷它們會:

for (var ln in lineitems) { 
// whatever you want to do with a line. 
} 

到acccess通過指數的具體項目是:

lineitems.Item(i); // get the ith item in the list. 
0

看來,你試圖通過設置值,像你在Javascript中做來增加數組的大小。 C#數組不是那樣的。您需要按完成後希望的尺寸創建它們。或者,您也可以使用List對象,使用Add()方法將新內容放入列表中。

2

這是因爲您創建了一個包含0個元素的數組,並嘗試在位置0上插入一個元素。這不起作用。您可以通過聲明的大小爲1的陣列開始與修復:

// Create an array for lineitems 
taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert[] lineitems = new taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert[1]; 

然而,調整上飛的數組是不是這樣做的慣用方式.NET。您可以使用一個List<T>,它負責爲您調整大小,併爲您留下更清晰的代碼,並可能會有更好的性能。

+0

是的,聲明大小爲1的數組已工作。我現在要查看列表,看看我是否可以清理代碼。謝謝! – Shazam

0

lineitems顯然與dtrHDR.GetChildRows("HdrLine")返回的行集合大小不同。您正在創建一個零元素數組,然後嘗試索引它。如果你希望它匹配dtrHDR.GetChildRows("HdrLine")的大小,那麼你需要先調用它,然後在獲得數值後初始化數組。

而不是使用數組,爲什麼不使用List<T>,只是推物品?無需再擔心IndexOutOfRange例外。

0

您需要在放入任何東西之前初始化數組。首先調用.resize。

0

這些線路都是問題

int ln; 
ln = 0; 

// Create an array for lineitems 
taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert[] lineitems = new taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert[ln]; 

創建0個元素的表。

0

你在你的程序有一個差一錯誤:

int ln = 0 
    . 
    . 
    . 
    taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert[] lineitems = new taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert[ln]; 
    . 
    . 
    . 
    lineitems[ln] = jel; 

要初始化0元件的陣列,然後試圖將第一元件(元件[0])設置爲一個值。