Wednesday 2 October 2013

String [ ] args

I'm about to delve into the world of C#, an Object Orientated language heavily influenced by Java, on my current studies at Birmingham City University. And as happened when I attended the Foundation Degree in Enterprise Computing (or Computer Enterprise - I don't recall which way about), we're starting from the ground and working up. This is good even at MSc level because, for whatever reason, programming and software development are subjects that seems to have many people confused. As far as I'm concerned, it's Databases that's the difficult bit; in comparison to this particular field, programming is relatively easy. (I said relatively...)

One that that I wondered with Java is why String [] args in the main declaration? It wasn't until the second year of the Foundation Degree that this was explained. You see, before Windows, Workbench, STOS, GEOS and othe GUIs, there was DOS (and before that, CP/M I think), and as this still exists to a fashion even in Windows.

Anyway, String [] args is in there in C# and allows you to pass arguments or parameters to your Command Line application. Sometimes you don't need to make a Windows-based application. So, how does it work?

Firstly, let's open up Visual Studio and make a C# Command Line application. It should give you some source code to get started, which will look something like this (notice that I've called my application TestConsoleApplication):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}
  

At the moment, the Main is the entry point of the application, and it doesn't do anything yet, so let's add some code there, like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            /// Sets the title bar on our console
            string consoleTitle = "Using Command Line"
                + "... Like a DOS!";
            Console.Title = consoleTitle;
            /// Set up a integer in local scope,
            /// initialising it to zero
            int counter = 0;
            /// Clears the console, like cls
            /// (CLear Screen)
            Console.Clear();
            /// Checks if there's any arguments first
            if (args.Length > 0)
            {
                /// If args has been added, then
                /// we'll loop through each one
                foreach (var arg in args)
                {
                    Console.Write(arg);
                    Console.Write(" ");
                    counter++;
                }
                /// Escape string for newline
                Console.Write("\n");
                Console.Write("\nNumber of arguments sent: "
                              + counter);
                Console.Write("\n");
            }
            /// Here's a generic message, like "Hello World"
            /// or whatever. Note: WriteLine adds a carriage
            /// return after writing the text
            Console.WriteLine("I can do Computers me!");
            Console.Write("Press the Any key to exit...");
            /// Wait for key-press
            Console.ReadKey(true);
            /* FIN - Exit application */
        }
    }
}
  

So, in Visual Studio, you have to build your solution before it will run: if you make changes without building, the compiled .exe will be the last build (and if you haven't built anything, nothing at all). Once built (and by default, it'll be a Debug version unless you state Release), you can run it from Command Line.

In my case, I put all my development stuff in C:\development\ - if it's PHP, there'll be a sub-folder in there called www, but this is windows, so that's the name of my sub-folder.

The path to the build in my case is c:\development\windows\TestConsoleApplication\TestConsoleApplication\bin\Release. From your start menu, type cmd.exe and navigate to your solution (if you've built a debug version, it will be in \bin\Debug not \bin\Release as here). If you type:

c:\your\path\to\bin\Release> dir *.exe

and press enter, you should see your built solution. In my case, it's called TestConsoleApplication.exe - so type your solution name in (I think the .exe is optional) and press enter.

As you've not sent any arguments to your solution (or program, or console application), it will not display anything. Try again, but this time, after the solution type Hello World and see what happens. Try any number of words or numbers, like The Quick Brown Fox Jumps Over The Lazy Dog and see what happens.

Okay, this is a trivial example, but it means that you can set any number of parameters in your program with a bit of thought. For instance, you could use a shell sort to display each argument alphabetically, but I'll leave you to think how one might do that.

No comments:

Post a Comment