Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6661911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:20:04+00:00 2026-05-26T02:20:04+00:00

I am trying to create a Collection with properties and their respective accessors. Here

  • 0

I am trying to create a Collection with properties and their respective accessors.

Here is my code:

class SongCollection : List<Song>
{
    private string playedCount;
    private int totalLength;

    public string PlayedCount
    {
        get
        {
            foreach (Song s in this)
            {
                if (s.TimesPlayed > 0)
                {
                    return s.ToString();
                }
            }
        }
    }


    public int TotalLength
    {
        get
        {
            foreach (Song s in this)
            {
                int total = 0;
                total += s.LengthInSeconds;
            }
            return total;
        }
    }
}

I’m receiving the error at the “get” point. It tells me that not all code paths return a value… What exactly does this mean, and what am I missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:20:04+00:00Added an answer on May 26, 2026 at 2:20 am

    Firstly, the reason you’re getting that message is that if this is empty, then the code within the foreach block (which is where the required return statement is) would never be executed.

    However, your TotalLength() function would always return the length of the first Song, as you’re declaring your variable, setting its value, then returning it within the foreach block. Instead, you’d need to do something like this:

    int totalLength = 0;
    
    foreach(Song s in this)
    {
        total += s.LengthInSeconds;
    }
    
    return totalLength;
    

    Your PlayedCount function suffers from similar issues (if the collection is empty or contains no elements whose TimesPlayed property is greater than 0, then there would be no way for it to return a value), so judging by your comment you could write it this way:

    public int PlayedCount()
    {
        int total = 0;
    
        foreach(Song s in this)
        {
            if (s.TimesPlayed > 0)
            {
                total++;
            }
        }
    
        return total;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to dynamically create a connection string during compile time: private static string
I'm trying to create a custom component with a collection property. However if I
I'm trying to use command objects in the following way: class BeneficiaryCommand { List<Beneficiary>
I am trying to create a dropdown list to display all the value in
I have the following code in which I'm trying to serialize a list to
This is mostly for educational purposes. I'm trying to create the InputMapper class, which
I'm trying to create a base class for my POCO objects in .NET 4,
I'm trying to create a TCP connection and send/read data that uses SSL, but
I'm trying to create an OleDb connection to an Excel file that is located
I'm trying to create a soap connection to Magento's web services, however I'm getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.