控制台中的C#错误
本文关键字:错误 控制台 | 更新日期: 2023-09-27 18:29:43
我有一个我玩的游戏的来源。一旦我登录游戏,控制台就会抛出一个错误。错误如下:
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);
}
我们将不胜感激!
请尝试使用此方法。
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
您收到此错误是因为当您进行以下调用时:
ChangeDigit(4, 0);
ID
的值长度不能少于3个字符。
例如,假设ID = "AB"
,当您在对ChangeDigit(4, 0)
的调用中调用Item.Remove(Place - 1, Item.Length - Place + 1)
时,它本质上是这样做的:
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是一个空字符串,或者由于代码中其他地方的错误而不是您所期望的ID?