2

Question of the day: Enums

Posted in C#, Daily Question at October 7th, 2009 by Gerrod / 2 Comments »

Some fun with enums! Assume you have an enum defined as follows:

enum Frequency {
    None = 0,
    Annual = 1,
    SemiAnnual = 2,
    Quarterly = 4,
    Monthly = 12,
    Weekly = 52
}

Now, given the following block of code:

Frequency myFrequency = (Frequency) 104;
System.Console.WriteLine("My frequency is: {0}", myFrequency.ToString());
bool is100Defined = Enum.IsDefined(typeof(Frequency), 100);
bool is52Defined  = Enum.IsDefined(typeof(Frequency), 52);
  1. What is written to the console?
  2. What is the value of is100Defined?
  3. What is the value of is52Defined?

First correct answer gets absolutely nothing (but think how great you’ll feel!)

0

What Is a Race Condition?

Posted in .Net, Asp.Net, Javascript at October 6th, 2009 by Drew / No Comments »

Just thought I would put a post out on something I learnt today that can occur when using ajax- a race condition. A race condition is is where the output or result of something is dependent on the timing of other events/code. A good example of this is below:

function ValidateInput() {
//some slow running code here...
}

function SaveForm() {
//save the form
}

<form>
<input type="text" onchange="ValidateInput()" />
<input type="button" onclick="SaveForm();" value="Submit" />
</form>

The problem here can occur when the user hits the Submit button before the validation method can finish. One way to overcome this is use a semaphore to indicate whether the form still needs validating on the SaveForm method.