Search code examples
c#.netcsla

add test to splint method


I am new to c# programming. Could someone please help me find out how to add a second test to this code :

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

It ruturn an exception "System.IndexOutOfRangeException: Index was outside the bounds of the array. "in the case CalcInsonorisation_Desc is null.


Solution

  • You can try

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