Happy Birthday (The C-Sharp way)
This evening I wanted to wish someone a happy birthday. In my infinate geekdom, I figured writing a tiny app to do it would be a nice way to waste 20 mins this evening. I started off writing in Notepad (because this really isn’t worth opening Visual Studio for), and compiled using CSC jsut to make sure it worked in the end (oops, forgot a few semi-colons…)
using System;
public class BirthdayWisher
{
public BirthdayWisher(string personName, DateTime birthDate)
{
this.PersonName = personName;
this.BirthDate = birthDate;
}
public readonly DateTime BirthDate;
public readonly string PersonName;
const string BirthdayMessage =
"\r\nDear {0}! Wishing you a happy birthday for {1}. Congratulations on being {2} years old!";
public void WishBirthday()
{
Console.WriteLine(String.Format(BirthdayMessage, PersonName, BirthDate.ToString("MMMM dd"), DateTime.Today.Year - BirthDate.Year));
}
}
public class NaomiBirthday
{
[STAThread]
public static void Main(string[] args)
{
BirthdayWisher naomi = new BirthdayWisher("Naomi", new DateTime(1983, 11, 21));
naomi.WishBirthday();
Console.WriteLine("\r\nPress any key to end.");
Console.ReadKey();
}
}
So i sent her an IM message with that code and the following instructions to run this:
1. Copy the text above and save it to your desktop as a file called “birthday.cs”
2. Click “Start –> Run” and paste the following into the box and then press enter.
%windir%\system32\cmd.exe /c “%WINDIR%\Microsoft.NET\Framework\v2.0.50727\csc /out:”%USERPROFILE%\birthday.exe” “%USERPROFILE%\desktop\birthday.cs” > null&&”%USERPROFILE%\birthday.exe”&& del “%USERPROFILE%\birthday.exe” ”
Yes, i am a massive geek, but now I can re-use the same code for someone else’s birthday - yay for reuse! The harsest part about this whole thing wasn’t the code, but in the command-prompt syntax required to chain commands properly and get it to execute as i’d expect. Even after spending maybe 30 mins on it, I don’t think I got it right, and certianly it could be improved upon….I guess that leaves room for Happy Birthday v2
Related posts:
- Thread Safety and Locking I was recently reading a post about writing non-threadsafe code...
- DateTime.Parse() is locale sensitive The DateTime.Parse() method (and all its derivatives, i assume) are...



