Im currently trying to make a new function inside of a class (in C#) and inside I have
public static Planet searchByName(String nameIn)
{
for (int i = 0; i < planetList.Count; i++)
{
Planet returnPlanet = null;
if (planetList.ElementAt(i).name == nameIn)
{
returnPlanet = planetList.ElementAt(i);
}
if (returnPlanet == null)
{
throw new System.ArgumentException(String.Format("Planet with name {0} does not exist", nameIn));
}
return returnPlanet;
}
}
Im not sure how to throw the Exception and get it to return a value (or at least get rid of the current error)
Edit: Ah, I see, as many of you pointed out, the loop I have has a possibility of not running (although in my code it would always run, but I doubt the Compiler would know that)
If your
forloop condition (i < planetList.Count) is false, theforloop won't be run. You need toreturnsomething if theforisn't run.