2 Replies - 245 Views - Last Post: 30 July 2013 - 04:44 PM Rate Topic: -----

#1 ogadit  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 31-July 12

using switch with string[ ]

Posted 30 July 2013 - 02:50 PM

I am making a console application which opens names of programs i enter
i store the names of the programs in a string and then separate them into a string[] like this:
string app = "chrome,calc,notepad";
string[] apps = app.Split(',');


now i want to use switch to determine which application's name was inputed how can i do this??
I was using this code:
switch (apps)
{
case 0:
System.Diagnostics.Process.Start("chrome.exe");
case 1:
System.Diagnostics.Process.Start("calc.exe");
case 3:
System.Diagnostics.Process.Start("notepad.exe");
}


Regards,
ogadit

Is This A Good Question/Topic? 0
  • +

Replies To: using switch with string[ ]

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8962
  • View blog
  • Posts: 33,586
  • Joined: 12-June 08

Re: using switch with string[ ]

Posted 30 July 2013 - 02:53 PM

Why not store them with a 'dictionary' collection object? You specify a key (this case the program name), and then the value (the program's executable's directory).

http://msdn.microsof...y/xfhwa508.aspx
Was This Post Helpful? 1
  • +
  • -

#3 Witchking  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 188
  • Joined: 17-February 13

Re: using switch with string[ ]

Posted 30 July 2013 - 04:44 PM

Like modi123_1 suggests, i would use a Dictionary<string, ProcessStartInfo>, storing each process' ProcessStartInfo object under a string key. This way you can also avoid hardcoding each file, and can add more without having to recompile.

To get input from the user you could use the Console.ReadLine method.

This post has been edited by Witchking: 30 July 2013 - 04:45 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1