Archive for September, 2007

Using App.Config for user defined runtime parameters

September 27, 2007

These are the steps involved in defining runtime parameters and using them in your code.

1. Create a App.Config file
2. Define the runtime parameters and provide values for these
3. Access these parameters in your code
4. Verify changing the parameter value at runtime affects the application


Step 1: Create App.Config file

1a. Create a Console Application
1b. In Solution Explorer, right click on project. Add->New Item. Select Application Configuration file. This file will be added to your project.


Step 2: Define the runtime parameters and provide values for these

2a. Open App.Config.
2b. Define the keys and values for your runtime parameters.

Example:

<configuration>
<appSettings>
<add key="OperatorName" value="Rita"></add>
<add key="LoggerLevel" value="5"></add>
</appSettings>
</configuration>

You can define any key and provide values. Ensure you don’t have duplicate keys.


Step 3: Access these parameters in your code

3a. Add a refernce System.Configuration DLL.1
3b. Add the following lines in your code. You can directly add these lines in Main.

string name = System.Configuration.ConfigurationManager.AppSettings["OperatorName"];
Console.Writeline("Welcome " + name);
string level = System.Configuration.ConfigurationManager.AppSettings["LoggerLevel"];

Console.Writeline("Logger level: " + level);

3c. Build your project and execute it.The output should be:

Welcome Rita
Logger level: 5


Step 4: Verify changing the parameter value at runtime affects the application

4a. When you build your project, the App.Config file is copied to your Debug/Release folder with the name .exe.config. My project is named UseAppConfig. The App.Config in my Debug folder is UseAppConfig.exe.config

Open the .config file in your working directory (Debug/Release). Change the values to Mary and 1. In the same folder you will see your executbale. Double click on this and you should see the new values.2

You are all set. You can identify all those parameters that make sense as runtime parameters, define a meaningful key for each parameter and start using it. You no longer have to build your application every time you want to change the parameter value.


Notes

1: You need this reference only if you are using .NET 2.0
2: If you change the App.Config from Visual Studio IDE, then you need to rebuild your project to see the new values.

Ever heard of birthday blues?

September 11, 2007

It was my friend’s birthday yesterday. As usual, I didn’t realize it until he came up to me with a stretched out hand and said, “Today is my birthday. Wish me.” This rarely happens with me. My friends very well know I don’t care about birthdays and hence don’t bother to remember dates. My good friends don’t expect any wish from me and they move on as if nothing happened. My other category of friends (politically correct word for ‘bad’) know that I don’t remember their birthdays, and don’t remind me about it, but they still expect me to wish them, as if I will remember it all of a sudden.

Why is there an inherent assumption that you are good friends only if you remember each other’s important dates? I have never wished my best friend on her birthday. Does that mean I am not her friend? Knowing me, she doesn’t care – she just ignores this tiny incident (or the absence of it). Why is it so difficult for people to get this? If it is your birthday, you have all the right to celebrate your feat of burdening this world and its inhabitants with your existence. I might not appreciate it enough to participate in your celebrations. Okay? Good.

Why is it that every birthday comes attached with this custom of giving gifts? I hate it when someone gives me a gift. I am obligated to reciprocate and buy something equally aweful (if I can’t find something more awful) for them. Why not do away with this ritual? For all those who know me, if you give me a gift, don’t expect the same from me. I might not even remember about your gift the next day, forget remembering it till your birthday!

Another annoying thing is ‘treat’. ‘Treat me because it’s your birthday’. Either that or get loads of sweets so that your friends can gorge on it. This is being on the other side of the fence. Why do you assume I am celebrating this day and even if I am celebrating, what makes you think I want to include you in that?

Something which I was not aware of until recently: face painting. For ignorant people like me, face painting refers to that vulgar act of smearing the birthday boy’s (or girl’s) face with cream taken off the cake. This is the silliest of the lot. First off, it’s cream – it’s edible, don’t waste it. Think of all those people who don’t have a square meal in months and here you squander because you want to have fun. Secondly, these things shouldn’t be done in your workplace. Heard of the word professionalism? Bet you haven’t. Third point, of course, is to follow this ritual for every soul in your team. Ask the poor guy if he wants to celebrate his birthday at all?

Sun signs. They are side-effects of birthdays, right? What on earth can you find out about a person by knowing his/her sun sign? Oh you are a Taurian? oh good… we will get along well. You are a scorpion? I better stay away from you. This is more prevalent among the fair skinned species. I mean, give me a break! You choose friends depending on their sun signs?

With all these qualms I have, I cringe whenever someone asks me about my birthday. I have decided I am going to say ‘February 29′. Yeah, I thought of February 30th, but some people are actually smart enough to see through this trick. February 29th it is. That way, I have to endure only one fourth of this torture.

Disable Close button in Windows form

September 7, 2007

To disable that little close button in your Windows form, either you can set the ControlBox field in Properties window to False or or add this two line in the contructor:

this.ControlBox = false;

Same goes for Maximize and Minimize boxes. Set it using Properties window or add these lines:


this.MaximizeBox = false;
this.MinimizeBox = false;

Simple, huh?