【问题标题】:Passing parameters in a method, in an if-else statement C#在方法中传递参数,在 if-else 语句中 C#
【发布时间】:2016-03-02 15:57:36
【问题描述】:

我正在编写一个可以接受一定数量参数并包含 if-else 语句的方法。当我使用命令行参数时,我传递的参数最少为 3 个,最多为 4 个。

当我只使用 3 个参数运行命令行时,它应该只运行 if 的第一部分。只有当我必须传递第四个参数时,它才会运行 else,但是每次我运行四个参数时,代码都不会进入 else,而只会运行 if 语句的开头。任何想法表示赞赏

protected void net_group(string command, string param1, string param2, string param3)
        {


Console.WriteLine("Got information net group command");

        //creates group.txt file when "net group" command is used
        string path = "C:\\Files\\groups.txt";
        using (StreamWriter sw = File.AppendText(path))
        {
            sw.WriteLine(param2 + ": " + param3); //param2 is name of the group //param3 is name of user
        }
            if (not sure what argument would go here) {

                //writes to the audit log and to the console when group is made w/out users
                Console.WriteLine("Group " + param2 + " created");
                string path2 = "C:\\Files\\audit.txt";
                using (StreamWriter sw2 = File.AppendText(path2))
                {
                    sw2.WriteLine("Group " + param2 + " created");
                }
            }
            else
            {
                //writes to the audit log and to the console when user is added to a new group


//currently the method wont reach here even when I pass four parameters

                Console.WriteLine("User " + param3 + " added to group " + param2 + "");
                string path3 = "C:\\Files\\audit.txt"; //doesnt write this to audit.txt
                using (StreamWriter sw3 = File.AppendText(path3))
                {
                    sw3.WriteLine("User " + param3 + " added to group " + param2 + "");
                }
            }
            Console.Read();

【问题讨论】:

  • 请提供整个方法体,从声明开始
  • 添加了该方法的其余部分。只需注意“网络组”是第一个和第二个参数。第三个参数如“admin”将创建一个管理员组,添加第四个参数“alice”会将 alice 添加到管理员组
  • 那么使用@arghyaC 提供的答案将不起作用。让我再给你一个答案

标签: c# if-statement methods parameter-passing command-line-arguments


【解决方案1】:

查看方法的签名,最好的解决方案是使用类似以下内容的东西:

if (String.IsNullOrEmpty(param3)) // you could say that only 3 params were given
{

}
else // you could say that all 4 params were given
{

}

【讨论】:

  • 很高兴这是一个简单的修复。谢谢!
【解决方案2】:

this Microsoft tutorial

我想你的代码应该是这样的:

    static void Main(string[] args)
    {
        ...
        if (args.Length == 3) {

        //writes to the audit log and to the console when group is made w/out users
        Console.WriteLine("Group " + args[1] + " created");
        string path2 = "C:\\Files\\audit.txt";
        using (StreamWriter sw2 = File.AppendText(path2))
        {
            sw2.WriteLine("Group " + args[1] + " created");
        }
    }
    else
    {
        //writes to the audit log and to the console when user is added to a new group

正如教程指出的那样,您还可以使用 Environment.CommandLine 或 Environment.GetCommandLineArgs 从控制台或 Windows 应用程序中的任何位置访问命令行参数。

【讨论】:

    猜你喜欢
    • 2018-01-21
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    相关资源
    最近更新 更多