+ Reply to Thread
Results 1 to 2 of 2

Thread: No return value

  1. #1

    No return value

    I have that code that
    1) Check each item in the list for the name.
    2) If the name matches, return the item to the caller and exit the loop.
    3) If now found , throw an exception to indicate that the ball wasn't found.
    Code:
     public BallData GetBall(String Name)
        { 
            foreach (BallData Item in _BallList)
            {
                if (Item.Name == Name)
                {
                    return Item;
                }
                else
                {
                    throw new KeyNotFoundException("The ball name doesn't exist.");
                }
            }
        }
    But now Not all code paths return a value

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    40
    You can change your code to that modified one :
    Code:
    foreach (BallData Item in _BallList)
     {
                if (Item.Name == Name)
                {
                    return Item;
                }
      }
      throw new KeyNotFoundException("The ball name doesn't exist.");

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts