Home > IT & Software > Happy Birthday (The C-Sharp way)

Happy Birthday (The C-Sharp way)

October 23rd, 2008

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 =
        "rnDear {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("rnPress 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%system32cmd.exe /c “%WINDIR%Microsoft.NETFrameworkv2.0.50727csc /out:”%USERPROFILE%birthday.exe” “%USERPROFILE%desktopbirthday.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 :)

Bookmark this post:
  • DotNetKicks
  • DZone
  • TwitThis
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • Slashdot
  • StumbleUpon
  • Technorati

Related posts:

  1. Thread Safety and Locking I was recently reading a post about writing non-threadsafe code...
  2. Strongly-Typing Your Domain Values I love this one. I don’t know the name of...
  3. DateTime.Parse() is locale sensitive The DateTime.Parse() method (and all its derivatives, i assume) are...
  4. Upgrading Database Schema’s in .NET This post is the first what I intend to be...
  5. Unsatisfied By My Specification Pattern This week, I hit an interesting problem which I don’t...

Xerxes IT & Software ,

  1. No comments yet.
  1. No trackbacks yet.