Donavon Decker
Donavon Decker

Reputation: 39

C# Error In Console

I have a source for a game I play. The console throws an error once I log in the game. Here is the error:

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

Here are the codes:

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);
                    }



Help will be appreciated!

Upvotes: 0

Views: 1008

Answers (3)

Robbie
Robbie

Reputation: 19500

You are getting this error because when you make the following call:

ChangeDigit(4, 0);

The value of ID cannot be less than 3 characters in length.

For example, say ID = "AB", when you then call Item.Remove(Place - 1, Item.Length - Place + 1) within the call to ChangeDigit(4, 0) it will essentially be doing this:

int startIndex = 4 - 1 // 3
int count = 2 - 4 + 1 // -1
"AB".Remove(startIndex, count)

As you can see here, the count argument is -1 which would result in the exception you are getting Count cannot be less than zero.

You need to bring your program up in the debugger and take a look at what the ID value is at the time the exception is being thrown. Perhaps the ID is an empty string, or not what your expecting it to be because of a bug somewhere else in your code?

Upvotes: 0

aKzenT
aKzenT

Reputation: 7895

Try using this method instead.

   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);
   } 

If it throws an exception it means the ID is wrong/empty.

Upvotes: 1

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50732

Check if Item.Length - Place + 1 is greater than 0

Upvotes: 0

Related Questions