This is a really weird one and kinda difficult to explain, so stay with me.
I had some pretty basic c# code which runs based on an inputted string, then that string is used put into a logger and then it does some searches etc with it.. details are somewhat unimportant.
So, it keeps throwing a "Index was outside the bounds of the array." error even though for testing purposes I am manually setting the string array right before it uses it.
args[0] = "{XXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}";
logger.Info("guid:" + args[0]); //Errors here
_fetchxml = _myApp.getFetchXml("fileguid", args[0], new Guid(), new Guid());
I even just tried putting a line before the logger which just said var a = "a"
, and it gave the same error on that line, which makes me think its something with the build?
I have cleaned and rebuild the solution but with no luck, hopefully this makes sense.
An array is immutable (i.e. fixed size), so if the Length of the array is zero, it will throw an exception if you try to add a string and assign it to its first position (which is non-existent).
As suggested in the comments, you can simply confirm if indeed args.Length is 0 by adding an if-block:
if(args.Length > 0)
{
args[0] = "{XXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}";
logger.Info("guid:" + args[0]); //No more Errors here?
_fetchxml = _myApp.getFetchXml("fileguid", args[0], new Guid(), new Guid());
}
else
{
logger.Info("guid: --no arguments found--");
// or if _fetchxml is crucial:
throw new ArgumentException("No guid given");
}
Or alternatively, if it's only for testing, you could replace the args array entirely:
args = new[] { "{XXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" };
Or better yet, set the argument in the debug section of the project properties under 'Start options' in 'Command line arguments' (assuming these are indeed command line arguments of course).