The Lottery-Console-Application used at the last WPF-Event @ Microsoft Usergroup Switzerland
Last week we had a great WPF-afterwork-event at Microsoft Usergroup Switzerland (MSUGS) sponsored by Trivadis. I gave a deep-dive session about developing custom controls using WPF with many of it’s features like Dependency Properties, Commands, Routed Events, PART-Elements, Theme-Styles and so on.
At the end of the session a copy of my German WPF-book was drawn. We made a small lottery, but not a normal one. As all attendees were .NET-developers, everyone trusted in .NETs Random-class, so why don’t do it just in code?! We’ve written life a small console-application that did the job for us. As some people asked for the code (of course only the loosers and not the winner of the book ;-) ), here it is:
static void Main(string[] args)
{
Console.WriteLine();
Console.WriteLine(" --- MSUGS- WPF-Book Lottery");
Console.WriteLine(" Enter names and a dot (\".\")"
+ " when names are complete");
Console.WriteLine();
List names = new List();
Console.Write(">");
string name = Console.ReadLine().Trim();
while (!name.Equals("."))
{
names.Add(name);
Console.Write(">");
name = Console.ReadLine().Trim();
}
Random random = new Random();
int winnerIndex = random.Next(names.Count);
string winner = names[winnerIndex];
Console.WriteLine();
Console.WriteLine("Starting lottery...");
Thread.Sleep(3000);
Console.Write("And the winner is");
for (int i = 0; i < 10; i++)
{
Thread.Sleep(1000);
Console.Write(".");
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(winner);
Console.ReadLine();
}
The Console-Application looks like this:
The lucky winner at the MSUGS-event was Stefan, he was at index zero in the names-Collection. Congratulations!
Thanks to MSUGS for the great organization and to all developers who attended in the session. I hope you enjoyed it.
Leave a Reply