Tuesday, June 24, 2008

Dynamic holidays in app.config

Ever need to check a date against a list of holidays? Well here you go.


Create the following Supporting classes:



public class HolidayElement : ConfigurationElement
{
[ConfigurationProperty("Date", IsKey = true, IsRequired = true)]
public DateTime Date
{
get { return (DateTime)this["Date"]; }
}
}

public class HolidayElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new HolidayElement();
}

protected override object GetElementKey(ConfigurationElement element)
{
return ((HolidayElement)element).Date;
}
}

public class HolidayConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("Holidays")]
public HolidayElementCollection Dates
{
get { return (HolidayElementCollection)this["Holidays"]; }
}
}



You now can create a method to test for the date:




public static bool IsHoliday(DateTime dt)
{
HolidayConfigurationSection section =
(HolidayConfigurationSection)System.Configuration.ConfigurationManager.GetSection("HolidayConfigurationSection");

foreach (HolidayElement holiday in section.Dates)
{
if (DateTime.Compare(dt, holiday.Date) == 0)
return true;
}

return false;
}




Add the following to the config file (app.confg):



<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="HolidayConfigurationSection" type="TheApplication.Utility.Classes.HolidayConfigurationSection, TheApplication.Utility.Library" requirePermission="false" />
</configSections>
<HolidayConfigurationSection>
<Holidays>
<add Date="1/1/2008"/>
<add Date="5/26/2008"/>
<add Date="7/4/2008"/>
<add Date="9/1/2008"/>
<add Date="11/27/2008"/>
<add Date="11/28/2008"/>
<add Date="12/25/2008"/>
<add Date="12/26/2008"/>
</Holidays>
</HolidayConfigurationSection>
</configuration>



That's it. It makes it easy to change the date via the config file.
Enjoy...

Friday, June 13, 2008

Cool Power Commands for VS 2008

For those developers out there, here is a cool extension for your Visual Studio 2008! PowerCommands extremely useful and you wonder why Microsoft did not build these functions into VS the first time.

Ever need to add the same references to several projects in the same solution, yes this has copy and paste references!

Ever need to get to the directory where the file you are working on is, yes it has Open Containing Folder!


Microsoft did create it, but it should be built in to VS 2008. Better late than never…

Thursday, June 12, 2008

What to do, for President?

Like many, I think this political season is more of a blah. Neither candidate follows the ideologies of the founding fathers of this great nation..

Yes one candidate, John Sidney McCain III, served our country and was willing to pay the ultimate price, his life, to promote the liberty we take for granted, and for that I am thankfull. But he wanders the middle of the road on so many issues. See John's voting record here

The other, Barack Hussein Obama, Jr., really got where he is by default and a political machine. By default I mean he won his seat in congress because his opponent, Jack Ryan, dropped out because of a messy divorce from Jeri Ryan from Star Trek: Voyager, Seven of Nine. Way to go Jack. So not much experience here, just a lot of socialist thinking. See Barack's voting record here

So basically we have two candidates, which I commend for their convictions, but they are not really winners.

Is it time for us to revisit the following?

"When in the Course of human events it becomes necessary for one people to dissolve the political bands which have connected them with another and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.

We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness. — That to secure these rights, Governments are instituted among Men, deriving their just powers from the consent of the governed, — That whenever any Form of Government becomes destructive of these ends, it is the Right of the People to alter or to abolish it, and to institute new Government, laying its foundation on such principles and organizing its powers in such form, as to them shall seem most likely to effect their Safety and Happiness." Read more here.

Basically we need someone who will do what is best for all of the country, not just the poor or the rich.