System.IndexOutOfRangeException: 索引超出了数组界限

ccds 2013-04-29 08:40:06
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]都是有正确的值得
...全文
404 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ccds 2013-05-01
  • 打赏
  • 举报
回复 1
搞定了,果然是我的问题,确实有一个值没有- 多谢楼上各位了
wubing1111 2013-04-30
  • 打赏
  • 举报
回复
按你现在PROP有“-”的话 正确,如果这个变量传的值没有“-”的话,你用stat[1]就超出索引了啊,这样取的值只有一个啊,
宝_爸 2013-04-30
  • 打赏
  • 举报
回复
prop 中没有 ‘-’ if ((stat.Length > 1) && (stat[1] != "0")) itemstats.OtherProp += stat[0] + "\t" + stat[1] + "-";
bdmh 2013-04-30
  • 打赏
  • 举报
回复
索引超岀了,对于数组你好歹要注意一下
EnForGrass 2013-04-29
  • 打赏
  • 举报
回复
应该不会吧, abc-123看看是不是你这个-是英文的,分割的那个字符是中文的,

110,547

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧