I’m working on a form that needs three dropdowns for a user to input a date like this:
Rather than get out my ctrl+c, ctrl+v skills, I thought it’d be fun to play with LINQ in C#. First off, I’ll say that I know this is not the most efficient code, yes I know there’s hidden loops when it calls ToList() and Enumerable.Range() I was going for very succinct code that looked pretty, not performance.
The thing that makes this so nice is Enumerable.Range(int start, int count); This gets a continuous range of numbers with very little typing. And with each of those numbers just select a new ListItem. Another nice thing is to have quick ordering capabilities. I needed only the last 5 years from this year in descending order, that was easy with LINQ too. Once all the ListItems are created, it’s just a matter of converting the IEnumerable to an IList so we can call ForEach on it to add items to the dropdown all in one line of code.
protected void LoadDateDropdowns()
{
var months = from i in Enumerable.Range(1, 12)
select new ListItem()
{
Text = new DateTime(1900, i, 1).ToString("MMMM"),
Value = new DateTime(1900, i, 1).ToString("MMMM")
};
var days = from i in Enumerable.Range(1, 31)
select new ListItem()
{
Text = i.ToString(),
Value = i.ToString()
};
var years = from i in Enumerable.Range(DateTime.Now.Year - 5, 5)
orderby i descending
select new ListItem()
{
Text = i.ToString(),
Value = i.ToString()
};
// add the months to the dropdown
months.ToList().ForEach(i => ddlManufactureMonth.Items.Add(i));
// add the days to the dropdown
days.ToList().ForEach(i => ddlManufactureDay.Items.Add(i));
// add the years to the dropdown
years.ToList().ForEach(i => ddlManufactureYear.Items.Add(i));
}
When I was buying my car a few years ago there were two options for sound systems. Option 1, a single cd player with an external audio jack on the front, plus 4 dinky speakers using 50 watts total. Or option 2, a 6 disc mp3 cd player with no external audio jack, plus 8 speakers and a subwoofer using 465 watts. Well needless to say I went for the 465 booming watts, plus bone rattling bass. At the time I was seduced by all the speakers and the letters ‘mp3′ in the name, forgetting about the missing external audio jack. Little did I know that small little jack would come back to bite me. So for the past few years I’ve been burning mp3 cd’s with music on them, usually about 8 albums per cd. That music only lasts so long and I tire of it. Why not just burn more cd’s, you say? Well, I’m lazy. And now I have an iPhone. I’m wishing I had the audio jack now. My wife bought me one of those FM tuners to play audio from the iPhone in the car, but let’s be honest here, FM tuners suck. They always have and always will, no matter how many times I try them. This is no fault on my wife’s part, she did a good thing trying to feed my iPhone habit.
Anyways, that brings me to the point of this post. It’s that time of year when I break through my laziness and burn a new mp3 cd and move on to some new music. So here’s what I’ll be listening to for the next 6-12 months in the car. It’s an eclectic mix of sounds.
So the first thing to notice about this code is that there are two main concepts, a “multi caster” and a “handler”. “Handlers” have only one method called Handle() and you pass a magic string telling it what exactly to “handle”. This in itself is terrible because a developer has no idea what the possible values of what they should pass are unless they dig down into the Handle method to find where the other magic strings are stored. “Handlers” are configured through an arcane configuration framework that loads .dlls with concrete implementations of the Handler that have such methods as Invoke() and DoHandle().
Now it gets even better with the “multi caster”. In an application there may be any number of “Handlers” configured and since these handlers get invoked dynamically(something any other dependency injection framework could have done better) the best thing that the “multi caster” can do is to loop over all configured handlers and call Handle on ALL of them(whether they’re actually going to work or not). This is the really hilarious part where the IList of Exceptions comes into play. Since the multi caster just calls all the handlers blindly most are bound to fail and throw exceptions(there goes performance), so it returns a list of all those exceptions. What to do with all those exceptions is anyone’s guess. What’s worse is when the so called multi caster rips through each handle method some of them could get part way through execution and then throw leaving multiple handlers in an unkown state, perfect! The last part is just really annoying, if you only need one list of products, why return a list of lists of products and then just to add salt to the wound by only grabbing the first list out of the list of lists.
There are so many anti-patterns here it makes my head spin, but nobody else seems to think it’s a backwards way to accomplish something.
If you’re like me and like to see big pictures, you need to check out this blog from the boston globe http://www.boston.com/bigpicture/ It’s definitely one of my favorites.
Our company recently switched email domains, so to keep my old email address forwarding correctly I have to maintain the old account also. The problem is that I haven’t looked at it in a couple months. To my surprise I logged in today and found this:
Close to 60,000 emails! Granted, lots are error emails from our systems, but that’s still a lot of crap to sift through. Which reminds me of a great article about email I read recently at coding horror. I may never get out from under the pile.
I was alerted of this great tool last week that helps when you’re having problems with CSS based layouts. Check it out http://giveupandusetables.com/ It’s a timer that tells you when you’ve spent too much time trying to get your css layout to work in all browsers.
class HumanResources
{
private double revenue = ConfigurationSettings.AppSettings["InitialStartupCapital"];
static void Main(string[] args)
{
while (revenue > 0.0)
{
if (DateTime.Now.DayOfYear == 1) // first of the year
{
ChangeBenefitProvidersAndMakeEmployeesFillOutPaperwork();
Console.WriteLine("We're excited to offer these new and exciting benefits!");
// calculate new revenue
revenue -= ThisYearsRevenue() - LamePartiesFund() - DiversityFund();
}
else if (DateTime.Now.Day == 1) // first of every month
{
PlanLameParty();
Console.WriteLine("We're excited to offer these fun events this month!");
}
else if (DateTime.Now.Month % 3 == 0 && DateTime.Now.Day == 15) // quarterly
{
MakeEmployeesFillOutMeaninglessGoalsAndBaseTheirBonusesOnThem();
Console.WriteLine("We're excited to help you succeed in your career growth!");
}
Thread.Sleep(TimeSpan.FromDays(1)); // sleep until tomorrow
}
Console.WriteLine("We're sorry, you've been laid off.");
return 0;}
}
It all started out with $50 that my roommate Andy owed me. While we were driving to get some delicious Chipotle, Andy remembered that he owed me money from when I sold him my old phone and jokingly asked if I wanted to be paid in scratch lottery tickets. I laughed and told him, “yeah, why not?” and then we ate our huge chipotle burritos(mine of course had no beans because that would just be gross). A couple weeks later Andy was headed to the bank and said, “Are you sure your really want $50 worth of scratch tickets?” I had almost forgotten about it, but still stuck with my first answer. I told myself it’d be like an experiment or at the very least a good story. So a few hours later Andy walked into my room with $50 worth of scratch tickets.