0

I am having fun with a couple of errors I am getting in a C# application I am writing. The error I keep getting is:

  • encrypt and decrypt calls must have a return type
  • Console.WriteLine being used as a method
  • static void encrypt(string[] args) expected class, delegate, interface or struct
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string pw ="", hash =""; //Declare an intialise variables

            if (args.Length < 4) // Test to see if correct number of arguments have been passed
            {
                Console.WriteLine("Please use command line arguments in this format: encrypt -e (or -d) password-to-encrypt-with input-file output-file");
                Environment.Exit(0);
            }

            if (args[1].Length < 10 || args[1].Length > 40) // Test to see if the password is between 10 and 40 characters
            {
                Console.WriteLine("Please use a password between 10 and 40 characters");
                Environment.Exit(0);
            }

            switch (args[0]) //Uses first argument value to drive switch statement (-e or -d)
            {
                case "-e":
                encrypt(string[] args);
                break;

                case "-d":
                decrypt(string[] args);
                break;

                default:
                Console.WriteLine("When using the program please use -e to encrypt and -d to decrypt");
                break;
            }        
        } //End of MAIN

        static void encrypt(string[] args)  //Function to encrypt
        {
            string inputtext =""; //Initialise Varible (Ensure it is empty)
            inputtext=System.IO.File.ReadAllText(args[2]); //Read file in an assign to input text
            return;
        }

        static void decrypt(string[] args)  //Function to decrypt
        {
            string inputtext =""; //Initialise Varible (Ensure it is empty)
            inputtext=System.IO.File.ReadAllText(args[2]); //Read file in an assign to input text
            return;
        }
    }  
}

Any help would be much appreciated! Alistair

1
  • You do not need to initialize variables, just before you give them another value. Commented Nov 5, 2012 at 15:51

2 Answers 2

11

When calling a method, you must not specify the types of the arguments. So:

        case "-e":
        encrypt(args);
        break;
Sign up to request clarification or add additional context in comments.

Comments

2

Along with what Hans has said, you mentioned an error about return types in your methods.

Your encrypt and decrypt methods have return statements, but they are void methods meaning they don't have any return types.

Either give it a type you want to return (presumably the string you are manipulating) or just remove the return statement altogether. You do not need to explicitly put return at the end of a method to get it to exit out of the method. It will do that anyway.

Two small pro-tips, I would declare your fields on different lines, not all bunched together (with the way you have declared pw and hash) and also add a using directive for System.IO, so you don't have to call System.IO.File.ReadAllText, you can just call File.ReadAllText.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.