Click here to Skip to main content
15,900,714 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Stud_Information is red and this error :

'Students.Stud():' not all code paths return a value.

I call Students class in Stud method.But take this error.Why take this error message?

What I have tried:

Student.cs

namespace WorkList
{
    public class Stud_Information
    {
        public List<string> students= new List<string>();

        public static string Stud()
        {
            List<string> students = new List<string>();
            students.Add("Mary");
        }        
    }
}



Program.cs

namespace WorkList
{
    class Program
    {

        static void Main(string[] args)
        {
           Stud_Information.Stud();
            for (var i = 0; i < students.Count; i++)
            {
                Console.WriteLine("[" + i.ToString() + "] " + students[i] );
            }
            Console.ReadKey();
}

}
Posted
Updated 3-Mar-19 8:06am
v4

1 solution

Your Stud() method is defined as returning a string value which you are not returning.

Either return a string value in the method : return "Mary";

Or change the method definition to return void : public static void Stud()
 
Share this answer
 
Comments
[no name] 3-Mar-19 11:14am    
Yes you solved the first problem, but I think the next problem will be "the access" of a non static variable in a static function...
[no name] 3-Mar-19 12:21pm    
Hello.Yes this problem solved.Yes, I guess you had a problem.
Do you have any suggestions about what you can do about it? Thank you for your help.
[no name] 3-Mar-19 12:57pm    
Ignore what he said. There is no evidence to suggest that "books" is "non static" because it's not even in scope (according to your code). If you meant "students" then is not "non static"; it a "local" variable (which in this case has no relation to the "non static" defined in the class). A little knowledge is a dangerous thing.
[no name] 3-Mar-19 13:07pm    
Hi. When I check with debug in this way, the count is 0 in the following field.

for (var i = 0; i < students.Count; i++)

So while count = 1 it should be 0, because I can't reach the value in the class from another class.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900