Reputation: 809
"Not all code paths return a value"
public BallData GetBall(String Name)
{
//Check each item in the list for the name.
foreach (BallData Item in _BallList)
{
//If the name matches, return the item to the caller and exit the loop.
if (Item.Name == Name)
{
return Item;
}
else
{
// Otherwise, throw an exception to indicate that the ball wasn't found.
throw new KeyNotFoundException("The ball name doesn't exist.");
}
}
}
Upvotes: 0
Views: 82
Reputation: 25357
Change your code to:
foreach (BallData Item in _BallList)
{
//If the name matches, return the item to the caller and exit the loop.
if (Item.Name == Name)
{
return Item;
}
}
throw new KeyNotFoundException("The ball name doesn't exist.");
Upvotes: 5
Reputation: 30666
If _BallList
is empty, you'll never get into the loop and thus the method does not return anything
Upvotes: 3