我有一个游戏来源。登录游戏后,控制台会抛出错误。这是错误:
System.ArgumentOutOfRangeException: Count cannot be less than zero.
Parameter name: count
at System.String.RemoveInternal(Int32 startIndex, Int32 count)
at ConquerServer.Extra.ItemIDManipulation.ChangeDigit(Byte Place, Byte To) in C:\Documents and Settings\Administrator\Desktop\ConquerServer\ConquerServer\Extra.cs:line 134
at ConquerServer.Extra.ItemIDManipulation.ToComposeID(Byte EqPos) in C:\Documents and Settings\Administrator\Desktop\ConquerServer\ConquerServer\Extra.cs:line 235
at ConquerServer.Entities.Character.EqpStats(Byte Pos, Boolean Equip) in C:\Documents and Settings\Administrator\Desktop\ConquerServer\ConquerServer\Entities\Character.cs:line 1361
at ConquerServer.Entities.Character.SendExtra() in C:\Documents and Settings\Administrator\Desktop\ConquerServer\ConquerServer\Entities\Character.cs:line 1637
以下是代码:
public void ChangeDigit(byte Place, byte To)
{
string Item = Convert.ToString(ID);
string N = Item.Remove(Place - 1, Item.Length - Place + 1) + To.ToString();
N += Item.Remove(0, Place);
ID = uint.Parse(N);
}
if (EqPos == 1 || EqPos == 3)
{
ChangeDigit(4, 0);
ChangeDigit(6, 0);
}
Extra.ItemIDManipulation e = new Extra.ItemIDManipulation(Equipment[Pos].ID);
uint PID = e.ToComposeID(Pos);
if (Equipment[i].ID != 0)
{
MyClient.SendData(Packets.AddItem(Equipment[i], i));
EqpStats(i, true);
}
帮助将不胜感激!
答案 0 :(得分:1)
请尝试使用此方法。
public void ChangeDigit(byte Place, byte To)
{
string Item = Convert.ToString(ID, CultureInfo.InvariantCulture);
if(Place > Item.Length || Place < 1)
throw new ArgumentOutOfRangeException("Place");
Item = Item.Remove(Place-1) + To.ToString() + Item.Substring(Place)
ID = uint.Parse(Item, CultureInfo.InvariantCulture);
}
如果它抛出异常,则表示ID错误/为空。
答案 1 :(得分:0)
检查 Item.Length - Place + 1 是否大于0
答案 2 :(得分:0)
您收到此错误是因为当您拨打以下电话时:
ChangeDigit(4, 0);
ID
的值长度不能少于3个字符。
例如,说ID = "AB"
,当您在调用Item.Remove(Place - 1, Item.Length - Place + 1)
时调用ChangeDigit(4, 0)
时,它基本上会这样做:
int startIndex = 4 - 1 // 3
int count = 2 - 4 + 1 // -1
"AB".Remove(startIndex, count)
正如您在此处所看到的,count
参数为-1
,这将导致您获得Count cannot be less than zero
的异常。
您需要在调试器中启动程序,并查看抛出异常时的ID值。也许ID是一个空字符串,或者不是你期望的那样,因为代码中的其他地方有一个bug?