[英]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. 我正在写一个可以接受一定数量参数并包含if-else语句的方法。 When I am using Command Line arguments I am passing in a minimum of 3 parameters and a maximum of 4.
当我使用命令行参数时,我传入的参数最少为3个,最大为4个。
When I run my command line with only 3 parameters it should only run the first part of the if. 当我仅使用3个参数运行命令行时,应仅运行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.
仅当我必须传递第四个参数时,它才会运行else,但是每次运行四个参数时,代码都永远不会将其传递给else,而仅运行if语句的开头。 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();
Looking at the method's signature, the best solution would be to use something along the lines of this: 查看该方法的签名,最好的解决方案是使用一些类似的方法:
if (String.IsNullOrEmpty(param3)) // you could say that only 3 params were given
{
}
else // you could say that all 4 params were given
{
}
Look at this Microsoft tutorial . 查看此Microsoft教程 。
Your code would, I imagine look something like this: 我想您的代码看起来像这样:
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
As the tutorial points out, you can also use Environment.CommandLine or Environment.GetCommandLineArgs to access the command-line arguments from any point in a console or Windows application. 正如本教程所指出的,您还可以使用Environment.CommandLine或Environment.GetCommandLineArgs从控制台或Windows应用程序中的任何位置访问命令行参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.