Split - Index was outside the bounds of the array

后端 未结 7 552
野性不改
野性不改 2020-12-18 04:25

Im using the following code to split up a string and store it:

string[] proxyAdrs = linesProxy[i].Split(\':\');
string proxyServer = proxyAdrs[0];
int proxyP         


        
相关标签:
7条回答
  • 2020-12-18 04:47
    try
    {
        objCommonDD = new CommonDropDownBLL();
        objCommonDDEntity = new CommonDropdownEntity();
    
        //string strState=contextKey.ToString();
        string[] contextKeySplit = contextKey.Split('^');
        string strState = contextKeySplit[0].ToString();
        string strPin = contextKeySplit[1].ToString();
    
        objCommonDDEntity.TableName = "PCOM_PINCODES";
        objCommonDDEntity.DeleteField = "";
        objCommonDDEntity.TextField = "RTRIM(PIN_CITY_NAME) AS PC_DESC";
        objCommonDDEntity.ValueField = "DISTINCT PIN_CITY_CODE AS PC_CODE";
    
        objCommonDDEntity.StrCondition = " AND PIN_COUNTRY_CODE='IND' AND UPPER(PIN_CITY_NAME) LIKE UPPER('" + prefixText + "%') AND PIN_STATE_NAME='" + strState + "' AND PIN_CODE='" + strPin + "' ORDER BY PC_DESC";
    
        DataTable dtCity = new DataTable();
        dtCity = objCommonDD.GetData(objCommonDDEntity);
    
        string[] items = new string[dtCity.Rows.Count];
        int i = 0;
    
        for (i = 0; i < dtCity.Rows.Count; i++)
        {
            items.SetValue(dtCity.Rows[i]["PC_DESC"].ToString(), i);
        }
    
        return items;
    }
    
    0 讨论(0)
  • 2020-12-18 04:53

    There are two options that may help you, depending of whether or not you form incoming data (variable linesProxy):

    1. If you do form incoming data: Always include all parts of the string. In your case, always ensure you have 4 (assuming proxyAdrs[3] is the last) parts by adding additional : between 1st and 3rd values if no value for 2nd is provided. Thus after .Split() operation (ensure you don't activate RemoveEmptyStrings option ) your proxyAdrs[2] will be null and your sample will be fine.
    2. Otherwise: if proxyAdrs[2] is the only part the can be empty following snippet can prevent crashing:

      string[] proxyAdrs = linesProxy[i].Split(':');
      string proxyServer = proxyAdrs[0];
      int proxyPort = Convert.ToInt32(proxyAdrs[1]);    
      
      if(proxyAdrs.Length > 3)
      {
        if(proxyAdrs[2] != null)
            item.Username = proxyAdrs[2];
        if (proxyAdrs[3] != null)
            item.Password = proxyAdrs[3];
      }
      else
      {
       if(proxyAdrs[2] != null)
            item.Password = proxyAdrs[2];
      }
      
    0 讨论(0)
  • 2020-12-18 04:57

    Check the length of proxyAdrs before you attempt to subscript a potentially non-existent item.

    if ( proxyAdrs.Length > 1 ) {
      item.Username = proxyAdrs[2];
    }
    
    0 讨论(0)
  • 2020-12-18 05:03

    It is that your i which might be lower than the 2 you are trying to set in as index :)

    if i >= 2 then you can do all of the follwing:----

    if(proxyAdrs[2] != null)
            {
                item.Username = proxyAdrs[2];
            }
    
            if (proxyAdrs[3] != null)
            {
                item.Password = proxyAdrs[3];
            }
    }
    else I suggest you get out :D
    

    But again, checking proxyAdrs.Lenght would be the best.

    0 讨论(0)
  • 2020-12-18 05:07

    Try this:

            string[] proxyAdrs = linesProxy[i].Split(':');
            string proxyServer = proxyAdrs[0];
            int proxyPort = Convert.ToInt32(proxyAdrs[1]);
    
    
            if(proxyAdrs.Length > 2 && proxyAdrs[2] != null)
            {
                item.Username = proxyAdrs[2];
            }
    
            if (proxyAdrs.Length > 3 && proxyAdrs[3] != null)
            {
                item.Password = proxyAdrs[3];
            }
    
    0 讨论(0)
  • 2020-12-18 05:08

    Just check the length of the array returned in your if statement

    if( proxyAdrs.Length > 2 &&  proxyAdrs[2] != null)
        {
            item.Username = proxyAdrs[2];
        }
    

    The reason you are getting the exception is that the split is returning array of size less than the index you are accessing with. If you are accessing the array element 2 then there must be atleast 3 elements in the array as array index starts with 0

    0 讨论(0)
提交回复
热议问题