Reputation: 1133
We have a customer who is reported receiving an error message along the lines of "StartIndex cannot be less than zero. Parameter name: startIndex". This is a standard error message usually thrown in the Substring
and Remove
functions of the String
class. Unfortunately, I am unable to get ahold of the data that this is failing on for them, so I'm forced to debug this by looking at the source code.
I have looked for all incidences of Substring
and Remove
and verified that their StartIndex parameter could not be less than 0. I searched for all uses of "startIndex" and verified that, in the two cases where they were being used as the first parameter of a string function, it could not mathematically be less than 0 (in both cases, it was given the return value of an IndexOf
function, but then a string Length
and the value of 2 were added to it, so 1 is the smallest value it could have).
This suggests to me that either this is an issue which was fixed in prior codebases, they have a nonstandard codebase, or I'm looking in the wrong place. Are there any other functions in C# that raise this sort of error text? Or is there any way in which it could be reporting the wrong input parameter? Am I just misunderstanding this error message?
Upvotes: 0
Views: 838
Reputation: 1062780
Here's 91 methods from System.dll and mscorlib.dll:
static class Program
{
static void Main()
{
var names =
GetMethodsWithParameter(typeof(object), "startIndex")
.Concat(GetMethodsWithParameter(typeof(Uri), "startIndex"))
.Distinct();
foreach(var name in names)
{
Console.WriteLine(name);
}
}
private static IEnumerable<string> GetMethodsWithParameter(Type assemblyOrigin, string name)
{
foreach(var type in assemblyOrigin.Assembly.GetTypes())
{
foreach(var method in type.GetMethods(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static))
{
if(method.GetParameters().Any(x => x.Name == name))
{
yield return type.FullName + "." + method.Name;
}
}
}
}
}
Upvotes: 4
Reputation: 125630
There is more than just Substring
and Remove
with startIndex
parameter:
String.IndexOf
/ String.LastIndexOf
String.IndexOfAny
/ String.LastIndexOfAny
String.Join
String.ToCharArray
Most of above with several overloads which contain startIndex
parameter.
You should definitely get a call stack, otherwise it will be really hard to find place where problem occurs.
Upvotes: 2