日期:2014-05-17  浏览次数:21120 次

System.IndexOutOfRangeException: 索引超出了数组界限
本帖最后由 ccds 于 2013-04-29 20:43:05 编辑
try
{
    string[] OtherPropArray = itemstats.OtherProp.Split('=');
    itemstats.OtherProp = "";
    foreach (var prop in OtherPropArray)
    {
        //prop = "abc-123"
        string[] stat = prop.Split('-');
        //Debug.WriteLine(stat.Length);   2
        if (stat[1] != "0")
           itemstats.OtherProp += stat[0] + "\t" + stat[1] + "-";
    }
    Logger.Instance.WriteItem("{0}-{1}-{2}-{3}-{4}-{5}-{6}-{7}-{8}-{9}", 
        itemstats.CharName,
        itemstats.InternalName,
        itemstats.ItemQualityLevel,
        itemstats.ItemType,
        itemstats.Level,
        itemstats.RequiredLevel,
        itemstats.WeaponHand,
        itemstats.ItemLink,
        itemstats.OtherProp,
        itemstats.Coinage);
    Send("ITEMSTATGOT");
    return;
}
catch (Exception ex)
{
    Send("Internal server error: " + ex.Message);
    Logger.Instance.WriteGlobal(ex.ToString());
    return;
}

为啥这句
if (stat[1] != "0")开始会提示System.IndexOutOfRangeException: 索引超出了数组界限啊
纠结,一直找不到原因
但其实stat[0],stat[1]都是有正确的值得

------解决方案--------------------
应该不会吧,
abc-123看看是不是你这个-是英文的,分割的那个字符是中文的,
------解决方案--------------------
索引超岀了,对于数组你好歹要注意一下
------解决方案--------------------
prop 中没有  ‘-’

if  ((stat.Length > 1) && (stat[1] != "0"))
     itemstats.OtherProp += stat[0] + "\t" + stat[1] + "-";
------解决方案--------------------
按你现在PROP有“-”的话 正确,如果这个变量传的值没有“-”的话,你用stat[1]就超出索引了啊,这样取的值只有一个啊,