Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch Statement with Strings C#

I need to write something that will get the start-up arguments and then do something for those start-up args, and I was thinking that switch would be good but it only accepts for ints and it has to be for a string

This isn't the actual code but I want to know how to make something like this work

namespace Simtho
{
    class Program
    {
        static void Main(string[] args)
        {
            switch (Environment.GetCommandLineArgs())
            {

                case "-i":
                    Console.WriteLine("Command Executed Successfully");
                    Console.Read;
                    break;
            }
        }

    }
}
like image 638
Jarred Sumner Avatar asked Dec 14 '22 01:12

Jarred Sumner


1 Answers

Environment.GetCommandLineArgs() returns an array of strings. Arrays cannot be switched on. Try iterating over the members of the array, like this:

namespace Simtho
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (string arg in Environment.GetCommandLineArgs())
            {
                switch (arg)
                {

                    case "-i":
                        Console.WriteLine("Command Executed Successfully");
                        Console.Read();
                        break;
                }
            }
        }
    }
}
like image 170
Ben Gartner Avatar answered Dec 15 '22 15:12

Ben Gartner