"Hello, world" on steroids

Let's take a rest from the Kindle and let's write some code =). The next blog entry will be the third and last of the cycle committed to Amazon's reader.

Since I'm recovering from sickness and currently unemployed I've begun a course to get the MCDP, or Microsoft Professional Certified Developer certification. As with every course, the beginning is somewhat slow: we've had about a week of theory and only today we've begun to write some code. Inevitably, that code has been the venerable "Hello, world". Since I have some previous experience under my belt, as the teacher was explaining to the rest of the pupils the details about solutions, code and compilation on Visual Studio 2010, I was trying some things and exploring the Console object a bit further. And here are the results:

The program is really simple: a blinking "Hello, world". Let's see how we get that effect on the C# console.

 static void Main(string[] args)
 {
     Console.Title = "BLINK demo!";
     Blink("Hello, world");
 }
 

this is the main method: from it we call the Blink method, and we set the Title of the console window.

 /// <summary>
 /// Function for making the text sent blink
 /// </summary>
 /// <param name="text"></param>
 private static void Blink(string text)
 {
     int i = 1; //Determine iterations
     do
     {
         //If we're in an even iteration
         //text color is white
         //if odd, black 
         
         if(i%2 == 0) 
             Console.ForegroundColor = ConsoleColor.White;
         else 
             Console.ForegroundColor = ConsoleColor.Black;
         //Center the cursor horizontally
         Console.CursorLeft = 
             (Console.WindowWidth/2)-(text.Length/ 2);
         //Center the cursor vertically
         Console.CursorTop = 
             Console.WindowHeight/2;
         //Writes the text at the cursor's position
         Console.Write(text);
         i++;
         //A little delay to avoid blinking too fast
         Thread.Sleep(150);
         //reset i to avoid overflow
         if (i >= 100) i = 0;
     } while (!Console.KeyAvailable); 
     //Ends the loop if striking any key
 }

The i variable is simply needed to keep a count of the do loop iterations. Not because we're particularly interested in knowing how many iterations we're having, but because we need to alternate the console's fore color between black and white to make our text blink. Upon entering the loop the first thing to do is asking if i is even, with the operation i%2 that founds the remainder of a division by two and comparing that remainder with zero. If they're equals, which is to say there's no remainder, the number contained in i is even and we set white as the color for the console's text. Otherwise is odd and we set black as the text's color. Since the console's background color is black, when we set the text (or fore) color to the same black, we'll make the text "disappear", which is exactly the effect we're looking for.

Setting Console.CursorLeft and Console.CursorTop to those values we position the console's cursor where we want to write, in this case centered horizontally and vertically; since we already know the console0's dimension thanks to the Console.WindowWidth and Console.WindowHeight properties.

Then two tricks: we use Thread.Sleep(150) to stop running the main (and only, in this case) thread of our application for 150 milliseconds, making the blink noticiable. Then some rough error checking: if we let the loop running there'll be a time when, eventually, the value of i will be too great to be contained in an integer variable, so when it reaches a random value we reset it to zero. Not exactly any random value, but an even value, since the first iteration of the loop was with an odd value.

Last we examine the exit condition for the loop: while(!Console.KeyAvaliable). This condition will run the loop as long as there are no keys pressed in the input buffer; that is, the loop will run indefinitely until the user strikes any key.

And that's it. A really simple example to surprise your teacher the next time you have to make a "Hello, world" in C#.

Ok, all right, I admit it: this post is a simple excuse to test the code formatting in the blog. Sorry for this. :)