-2

我是c#编程新手。有人可以帮我找出如何在此代码中添加第二个测试:

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];
   }

在 CalcInsonorisation_Desc 为空的情况下,它会产生一个异常“ System.IndexOutOfRangeException:索引超出了数组的范围。 ”。

4

3 回答 3

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];
    }
于 2013-11-01T11:10:41.150 回答
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];
        }
   }
于 2013-11-01T11:10:48.960 回答
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];
        }
}
于 2013-11-01T11:10:50.727 回答