"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. :)

Comments (12) -

6/15/2011 12:31:21 PM #

penile curvature correction karl dean download

"Sorry, no post matches your criteria..." fix please! as1sss3!

penile curvature correction karl dean download United States |

6/16/2011 8:28:48 AM #

Tarsha Archila

Establishing effective one-way links is all about obtaining people interested. Assume of real-life experiences or information regarding a man or woman or product. Be a part of a group dialogue and give which you understand. It could mean you can possess really vital interaction therefore getting relationships for your personal hyperlinks.

Tarsha Archila United States |

6/17/2011 3:11:18 AM #

tutorial

Love this blog also ^_^ how long has this blog been around? i have never seen it before!

tutorial United States |

6/17/2011 5:42:28 AM #

Gilberto Pilsner

the most critical issue when carrying out seo is get a great total amount of backlinks initially. when you possess a sufficient volume of back links, after that you can start optimizing in page links, keyword denseness and other aspects which affects your ranking.

Gilberto Pilsner United States |

6/19/2011 11:24:33 PM #

Bethel Blannon

Great post, loving apple and enjoying this app called Wet JUmp http://tinyurl.com/wetjumpapp

Bethel Blannon United States |

6/19/2011 11:31:27 PM #

Jonas Orzalli

The app store is always providing great apps, check out this app http://tinyurl.com/wetjumpapp

Jonas Orzalli United States |

6/21/2011 11:23:30 PM #

Phoenix Bankruptcy Attorney

Great day, may be here is towards problem solely well, i've been doing research concerning your web site which's why it is very plainly home. I have been developing a crisp new writings with each other with trying get back glimpse great, every time as well as feel products i've spoil it. Ideas about how onerous was viewed as the to site? Properly any individual considerably prefer me with no an occurence do that , on top of that total people boost web need to have of chewing up receiving this completed as well as?

Phoenix Bankruptcy Attorney United States |

6/21/2011 11:43:20 PM #

San Diego Plastic Surgeons

cheers for that  publishing, men, keep in the amazing operation

San Diego Plastic Surgeons United States |

6/21/2011 11:43:40 PM #

San Jose Bankruptcy Attorney

Neat submit . Give thanks to you for, going to my blog web page dude! I'll e mail you soon. I didn't realise which.

San Jose Bankruptcy Attorney United States |

6/22/2011 12:23:36 AM #

New York Divorce Attorney

I really like your blog and i truly respect the fantastic top quality content you are submitting right here for totally free for your online viewers. Can you inform us which blog platform you are utilizing?

New York Divorce Attorney United States |

6/22/2011 12:42:42 AM #

Philadelphia Plastic Surgeons

I am fairly usually on the lookout for excellent weblogs find here. This wonderful report gets me thinking. check back for new information

Philadelphia Plastic Surgeons United States |

6/22/2011 12:51:27 AM #

San Jose Bankruptcy Attorney

I know I'm a bit past due in contributing my suggestions but that  specific post produced me consider. They were an absorbing blog post. I have turn out to be a normal reader of one's world wide web site since I stumbled on your web site a though back again. I cannot say that I agree with almost everything you stated having said that it was emphatically enlightening! I should probably be back once more again soon.

San Jose Bankruptcy Attorney United States |

Comments are closed