This code works as needed for the task, only the if tree will be too large, what can be redone so as not to load hundreds of if tests? According to the task: (Windows Form application - C #) when on the cmd command line we access the application via the / h command, command 1 is executed, when we issue the 1 2 3 command to the / h / s / e commands, and the application should not be displayed. Only when you click on the application form should be loaded.

Code in Program.cs

static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var form = new Form1(); if (args.Contains("/h")) { form.Check1(); } if (args.Contains("/s")) { form.Check2(); } if (args.Contains("/e")) { form.Check3(); } else if (args.Contains("/h") == false && (args.Contains("/s")) == false && (args.Contains("/e") == false)) { Application.Run(new Form1()); } } 
  • I would render var form = Form1() before checking the conditions, do you need only one form? - vp_arth
  • While one, but it is trifles, I will correct the code in the topic, so as not to distract :) - Vitokhv
  • To be honest, I don’t understand what args.Contains("/h") == false does, and others ignore or forbid commands ... I can only understand patterns, so it’s hard to navigate. - Vitokhv
  • Not exactly the answer to your question, but I believe that the most correct approach is not to reinvent the wheel and parse the arguments manually, but to use one of the popular libraries for this purpose, it will make the code easier and more readable, besides insure against stupid errors. Here is a thread in English SO: stackoverflow.com/questions/491595/… - Anton Papin

2 answers 2

Each call to contains makes the passage through the array, I think it is better to make one manually.
You can create a boolean variable that, if necessary, will prevent the application from starting.

  var form = new Form1(); bool needRun = true; foreach (string arg in args) { switch (arg) { case "/h": form.Check1(); needRun = false; break; case "/s": form.Check2(); needRun = false; break; ... } } if (needRun) { Application.Run(form); } 

In case there are no parameters planned at all in the GUI, you can simply check their absence:

 if (args.Length == 0) Application.Run(form); 

args.Contains("/h") == false checks that "/ h" was not passed in the command-line argument array.

  • one
    so that the savings become noticeable you need quite a lot of elements in the array. Considering that these are command line parameters there are unlikely to be so many. - Grundy
  • @vp_arth thanks, I will test your code, try to find logic in it, because new to this business :) and if it doesn’t make it difficult for you to add another / s command to the code for symmetry in the code so that I can see where the beginning is and where the end is for the case . - Vitokhv
  • @vp_arth tested, works fine, now there is a place for many commands, the only thing I did not understand is why Application.Run(form); instead of Application.Run(new Form1()); How does this affect the form display? - Vitokhv
  • We have already created a form, why create it again? - vp_arth
  • one
    @vp_arth is now reached :) through var created, just thought that this is a call to the form, not a creation. Thanks for the tips, they helped a lot, I was looking for pieces of the code for the task for a week :) - Vitokhv

I can only tell in which direction to move. In general, any trees of if-conditions need to be changed to associative arrays (arrays of key-value pairs). As a key, you have a string from args.Contains (), as a value, or as separate objects that call FormCheck (). Or different copies of one class, with different parameters. Depending on how not the same type of team.

It turns out something like:

 //объекты, вставляемые в "значения" interface Command { public void Run(Form1 form); } public class Command1 implements Command { public void Run(Form1 form){ form.FormCheck1(); } } public class Command2 implements Command { public void Run(Form1 form){ form.FormCheck2(); } } //это ассоциативный массив Dictionary<string, Command> commands = new Dictionary<string, Command>(); //таким образом вбиваются пары ключ-значение commands.Add("/h", new Command1()); commands.Add("/s", new Command2()); //или объекты одного класса с разными параметрами var form = new Form1(); bool needRun = true; //далее упрощаем все дерево условий в одну строку foreach (string arg in args) { if (commands.ContainsKey(arg)) { commands[arg].Run(form); needRun = false; } } if (needRun) { Application.Run(form); } 
  • contains<string, bool> just a string[] args method string[] args . - vp_arth
  • @raingo thanks for the comments on your code, it is useful to understand the logic of their work. I was advised in one topic to do everything in a separate class, but I did not understand how to create a class :) - Vitokhv
  • Vitokhv, sucks. In this case, my answer is unlikely to help = ( - raingo
  • @raingo, The idea is excellent, I tried to bring to mind, accept the edit) - vp_arth
  • vp_arth, edit accepted. THX. But I feel now the code is even more confusing. I do not have enough competences to stupidly understand, is Form1 - is this its class or default? And what does Check1 (), Check2 () do. - raingo