2013-11-01 62 views
-2

我是c#編程新手。可能有人請幫助我瞭解如何進行第二次測試添加到這個代碼:add test to splint method

if (item.CalcInsor_Desc != null) 
    { 
     string[] CalcInsor_Desc = item.CalcInsor_Desc.ToString().Split('.'); 
     schema2.CalcInsonorisation_TypeCode = CalcInsor_Desc[0]; 
     schema2.CalcInsonorisation_Desc = CalcInsor_Desc[1]; 
    } 

它ruturn例外「System.IndexOutOfRangeException:索引是該數組的範圍之外」的情況下CalcInsonorisation_Desc爲空。

回答

0

您可以嘗試

if (item.CalcInsor_Desc != null) 
{ 
    string[] CalcInsor_Desc = item.CalcInsor_Desc.ToString().Split('.'); 
     if (CalcInsor_Desc.Length >= 2) 
     { 
      schema2.CalcInsonorisation_TypeCode = CalcInsor_Desc[0]; 
      schema2.CalcInsonorisation_Desc = CalcInsor_Desc[1]; 
     } 
} 
0

檢查數組包含最低要求的元素

if(CalcInsor_Desc.Length>1) 
{ 
schema2.CalcInsonorisation_TypeCode = CalcInsor_Desc[0]; 
schema2.CalcInsonorisation_Desc = CalcInsor_Desc[1]; 
} 

或者

if(CalcInsor_Desc.Length=1) 
    { 
    schema2.CalcInsonorisation_TypeCode = CalcInsor_Desc[0]; 
    schema2.CalcInsonorisation_Desc = string.Empty; 
    } 
    if(CalcInsor_Desc.Length>1) 
    { 
    schema2.CalcInsonorisation_TypeCode = CalcInsor_Desc[0]; 
    schema2.CalcInsonorisation_Desc = CalcInsor_Desc[1]; 
    } 
0
if (!item.CalcInsor_Desc.Equals(null)) 
    { 
     string[] CalcInsor_Desc = item.CalcInsor_Desc.ToString().Split('.'); 
     if(CalcInsor_Desc.Length >= 2){ 
      schema2.CalcInsonorisation_TypeCode = CalcInsor_Desc[0]; 
      schema2.CalcInsonorisation_Desc = CalcInsor_Desc[1]; 
     } 
    }