samedi 28 novembre 2015

Passing parameters in a method, in an if-else statement C#

I am writing a method that can take in a certain number of parameters and contains an if-else statement. When I am using Command Line arguments I am passing in a minimum of 3 parameters and a maximum of 4.

When I run my command line with only 3 parameters it should only run the first part of the if. Only when I have to pass the fourth param it will run else, however everytime I run four parameters the code never makes it to the else and only run the beginning of the if statement. Any ideas are appreciated

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();

Aucun commentaire:

Enregistrer un commentaire