Home > Software engineering >  c# void return function error? Keep getting Public Main() method is required in a public class
c# void return function error? Keep getting Public Main() method is required in a public class

Time:11-13

using System;

class HelloWorld
{

    public static int GetYear()
    {
        Console.WriteLine("Please enter year: ");
        int year = Convert.ToInt32(Console.ReadLine());
        return year;
    }

    public static int GetMonth()
    {
        Console.WriteLine("Please enter month number (Febuary is 2): ");
        int month = Convert.ToInt32(Console.ReadLine());
        return month;
    }

    static string GetMonthName(int month)
    {
        string[] monthName = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
        string monthValue = monthName[month - 1];
        return monthValue;
    }

    public static bool LeapYear(int Year)
    {
        if (((Year % 4 == 0) && (Year % 100 != 0)) || (Year % 400 == 0))
            return true;
        else
            return false;
    }

    public static void print(string monthValue, int days, int year, int month)
    {
        Console.WriteLine("{0} {1} has {2} days\n", year, monthValue, days);
    }

    static void Main()
    {
        int[] days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        int[] leapDays = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        int monthDays;

        while (true)
        {
            int year = GetYear();
            int month = GetMonth();
            if (year > 0 && month > 0 && month <= 12)
            {
                string monthValue = GetMonthName(month);
                bool leap = LeapYear(year);

                if (leap)
                {
                    monthDays = leapDays[month - 1];
                    print(monthValue, monthDays, year, month);
                }
                else
                {
                    monthDays = days[month - 1];
                    print(monthValue, monthDays, year, month);
                }
            }
            else
            {
                Console.WriteLine("Invalid Year or Month entered!! Please enter a correct value");
                break;
            }
        }
    }
}

I kept getting an error in Github actions, the error was "could not find public class name" even though the Main class is capitalized. I don't know what I'm doing wrong here

When using dotnetfiddle as instructed by my teacher, the error message I received was "Public Main() method is required in a public class" but like I mentioned above, making sure the Main function was capitalized did nothing

CodePudding user response:

The error message you're receiving is because your Main method is not declared as public. In order for your program to run, the Main method must be declared as public.

To fix this, simply change the declaration of your Main method from:

static void Main()

to:

public static void Main()

CodePudding user response:

could not find the public class name

is Java error, you can refer to enter image description here

and if you really want to target lower .net versions then you can simply change the main method to public static void Main()

  •  Tags:  
  • c#
  • Related