简体   繁体   English

具有大量错误的C#应用​​程序

[英]C# Application with Numerous Errors

I am having fun with a couple of errors I am getting in a C# application I am writing. 我正在编写的C#应用​​程序中遇到一些错误,很开心。 The error I keep getting is: 我不断得到的错误是:

  • encrypt and decrypt calls must have a return type 加密和解密调用必须具有返回类型
  • Console.WriteLine being used as a method Console.WriteLine被用作方法
  • static void encrypt(string[] args) expected class, delegate, interface or struct 静态无效的void crypto(string [] args)预期的类,委托,接口或结构
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 阿利斯泰尔

When calling a method, you must not specify the types of the arguments. 调用方法时,不得指定参数的类型。 So: 所以:

        case "-e":
        encrypt(args);
        break;

Along with what Hans has said, you mentioned an error about return types in your methods. 除了Hans所说的,您还提到了有关方法中返回类型的错误。

Your encrypt and decrypt methods have return statements, but they are void methods meaning they don't have any return types. 您的encryptdecrypt方法具有return语句,但它们是void方法,意味着它们没有任何返回类型。

Either give it a type you want to return (presumably the string you are manipulating) or just remove the return statement altogether. 给它一个您想返回的类型(大概是您要操作的字符串),或者完全删除return语句。 You do not need to explicitly put return at the end of a method to get it to exit out of the method. 您无需显式地将return放在方法的末尾以使其退出该方法。 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 . 两个小技巧,我将在不同的行上声明您的字段,而不是将所有字段捆在一起(使用您声明pwhash ),并为System.IO添加using指令,因此您不必调用System.IO.File.ReadAllText ,您可以只调用File.ReadAllText

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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