|
 Tuesday, January 09, 2007

Hiding Code From The Debugger

Sometimes there are methods in code that my development department knows are robust. There are no bugs in the code. This might be helper methods that have been tested time and time again. Therefore we know we dont need to debug these methods 99% of the time. To alleviate the pain of stepping through unnecessary lines of code accidentally you can inform the debugger that it is not suppossed to access a block of code. To do this add the DebuggerHidden Attribue. You cannot set a break point in this block of code when this attribute is present.

[DebuggerHidden]
void DoSomething()
{
// Do Something
}

*Note: This is only valid on constructors, methods, properties, and indexers only.

#    Comments [0] |
 Friday, January 05, 2007

Conditional Switch Statement with Enumeration String Value

As of recently I've had a few requests for how I handle a string representation of an Enumeration in a conditional switch. If you pass in a string into the switch statement without parsing the enumeration .NET will complain about a constant value being required. Here's how to write switch statement with a string respresentation of the Enumeration. You might find this useful when you store a string value of the enumeration in a data source such as a DB or XML File and you need to get its Enumeration value through code.

A lot of the time developers will get an error when they first attempt to peform a conditional switch without knowing how to do it. The error they get is "a constant value is required". Here's how to actually implement it. :)

 

public enum Product
{
    Word,
    Outlook,
    Excel
}

public void Process(string productName)
{
    switch ((Product)Enum.Parse(typeof(Product), productName.ToString()))
    {
        case Product.Excel:
        // Do something
            break;
        case Product.Outlook:
            // Do Something
            break;
        case Product.Word:
            // Do something
            break;            
    }        
}
#    Comments [0] |