简体   繁体   中英

does not contain a static main method for suitable entry point

Keep getting the error message does not contain a static main method for suitable entry point. Would anyone be able to explain this error to me and possibly help me fix it? Thanks. New to C#

 {
    class Authenticator
    {

        private Dictionary<string, string> dictionary = new Dictionary<string, string>();

        public void IntialValues()
        {
            dictionary.Add("username1", "password1");
            dictionary.Add("username2", "password2");
            dictionary.Add("username3", "password3");
            dictionary.Add("username4", "password4");
            dictionary.Add("username5", "password5");
        }

        public bool Authenticate(Boolean authenticated)
        {
            Console.WriteLine("Please enter a username");
            string inputUsername = Console.ReadLine();

            Console.WriteLine("Please enter your password");
            string inputPassword = Console.ReadLine();

            if (dictionary.ContainsKey(inputUsername) && dictionary[inputUsername] == inputPassword)
            {
                authenticated = true;
            }
            else
            {
                authenticated = false;
            }

            return authenticated;
        }
    }
}

All executable programs must have a Main function somewhere in the project that is compiled to the exe.

If you just want to compile a class (eg to a dll) then you have to set that as the "project type" in visual studio.

The easiest way is to create a new project, but select class library as project type, and then paste your code in there. Alternatively you can use the command line to compile a file to a dll like so:

c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /target:library Authenticator.cs

If all of your code consists only of the block shown above then the error is more than clear. A Main method is required in your program.

The Main method is, by convention, the default point where the code starts executing. You can get here a more in depth explanation

So, for example, you need to add the following code

class Authenticator
{
    static void Main(string[] args)
    {
         Authenticator au = new Authenticator();
         au.InitialValues();
         if(au.Authenticate())
            Console.WriteLine("Authenticated");
         else
            Console.WriteLine("NOT Authenticated");
         Console.WriteLine("Press Enter to end");
         Console.ReadLine();

    }

    // Move the boolen variable inside the method
    public bool Authenticate()
    {
        bool authenticated = false;
        Console.WriteLine("Please enter a username");
        string inputUsername = Console.ReadLine();

        Console.WriteLine("Please enter your password");
        string inputPassword = Console.ReadLine();

        if (dictionary.ContainsKey(inputUsername) && dictionary[inputUsername] == inputPassword)
        {
            authenticated = true;
        }
        else
        {
            authenticated = false;
        }

        return authenticated;
    }
}

By the way, you should remove the parameter passed in input to the Authenticate method. You should declare it as an internal variable, set it depending on the outcome of the check and return it.

However, you could remove that variable altogether writing

    ....
    return (dictionary.ContainsKey(inputUsername)) && 
           (dictionary[inputUsername] == inputPassword)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM