Skip Navigation LinksHome > Products > Code Auditor > Screenshots > Tutorial

Code Auditor Logo

Fixing Sample Solution with Code Auditor

This tutorial will show you how to fix the sample "Regular Expression Editor" project that came with Code Auditor.

We will show you how to run Code Auditor on the sample solution and step you through each of the violated rules.

SSW Code Auditor Visual Studio Add-in Tutorial
  1. Getting started
  2. C# Code- Catch and re-throw exception improperly
  3. C# Code- Catch Exception must be more specific
  4. C#/VB.NET Code- Application entry method should handle "UnhandledException" and "ThreadException" events
  5. C#/VB.NET Code- Don't throw System.Exception
  6. C#/VB.NET Code- MessageBoxes must have icons
  7. C#/VB.NET Code- MessageBoxIcon.Question should not be used
  8. C#/VB.NET UI & Code- Buttons (except OK, Cancel, and Close), CheckBoxes, RadioButtons must have mnemonics
  9. C#/VB.NET UI & Code- OK, Cancel and Close buttons should not have mnemonics
  10. C#/VB.NET UI- FixedDialog must be used with CenterParent
  11. C#/VB.NET UI- Winform should have its own icon instead of using the default .NET application icon
  1. Getting started

    Follow these steps to start auditing your sample solution:

    1. Download and install Code Auditor (See: http://www.ssw.com.au/ssw/CodeAuditor/).
    2. Open the sample solutions from start menu/SSW Code Auditor.
      Image of Start Menu
      Figure: Starting sample solution from Start Menu/SSW Code Auditor
    3. Open NorthwindWindowsCS2005.sln in NorthwindWindowsCS2005 folder.
      Open NorthwindWindowsCS2005.sln
      Figure: Open NorthwindWindowsCS2005.sln
    4. Click "Audit" on the toolbar in Visual Studio.
      Health Auditor toolbar (a Visual Studio add-in)
      Figure: Health Auditor toolbar (a Visual Studio add-in)
    5. Select source code to scan and click "Start".
      Select project to scan
      Figure: Select project to scan
    6. Scanning...
      Scanning in progress...
      Figure: Scanning in progress...
    7. Finished
      Click "OK" to see the result
      Figure: Click "OK" to see the result
    8. The report will now open.
      Report in browser
      Figure: Report in browser
    9. Close the report, go back to Visual Studio and see the error report in Output panel.
      The result in Visual Studio Output panel
      Figure: The result in Visual Studio Output panel
      Note: Make sure the Output panel is visible.

    10. Continue with tutorial to start fixing code! :)
      Note: Double click on the error to navigate to error.

  2. C# Code- Catch and re-throw exception improperly

    Change from:
        throw ex;
    to:
        throw;

    See rule Do you catch and re-throw exceptions properly?.

  3. C# Code- Catch Exception must be more specific

    When an invalid regular expression is parsed in Regex.Match(), ArgumentException will be thrown - and this is what we want to catch.

    Change from:
        catch (Exception ex)
    to:
        catch (ArgumentException ex)

    See rule Do you catch and re-throw exceptions properly?.

  4. C#/VB.NET Code- Application entry method should handle "UnhandledException" and "ThreadException" events

    Add the highlighted line:

    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    And:
    private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        MessageBox.Show(e.ToString(), Application.ProductName + " " + new Version(Application.ProductVersion).ToString(2), MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    

    See rule Do you use exception management application block?.

  5. C#/VB.NET Code- Don't throw System.Exception

    Change from:
        throw new Exception("Not implemented yet; This is a test.");
    to:
        throw new NotImplementedException("Not implemented yet; This is a test.");

    See rule Do you know that you should never throw an exception using System.Exception?.

  6. C#/VB.NET Code- MessageBoxes must have icons

    Change from:
        MessageBox.Show("An error has occurred:" + Environment.NewLine +         Environment.NewLine +         ex.ToString(), Application.ProductName + " " + new Version(Application.ProductVersion).ToString(2), MessageBoxButtons.OK);
    to:
        MessageBox.Show("An error has occurred:" + Environment.NewLine +         Environment.NewLine +         ex.ToString(), Application.ProductName + " " + new Version(Application.ProductVersion).ToString(2), MessageBoxButtons.OK, MessageBoxIcon.Error);
    MessageBox without icon
    Figure: Bad - MessageBox without icon
    MessageBox with icon
    Figure: Good - MessageBox with icon

    See rule Do you know how to make message boxes user friendly?.

  7. C#/VB.NET Code- MessageBoxIcon.Question should not be used

    Change from:
        MessageBox.Show("File cannot be found.", Application.ProductName + " " + new Version(Application.ProductVersion).ToString(2), MessageBoxButtons.OK, MessageBoxIcon.Question);
    to:
        MessageBox.Show("File cannot be found.", Application.ProductName + " " + new Version(Application.ProductVersion).ToString(2), MessageBoxButtons.OK, MessageBoxIcon.Warning);
    MessageBox with question icon
    Figure: Bad - MessageBox with question icon
    MessageBox with warning icon
    Figure: Good - MessageBox with warning icon

    See rule Do you know how to make message boxes user friendly?.

  8. C#/VB.NET UI & Code- Buttons (except OK, Cancel, and Close), CheckBoxes, RadioButtons must have mnemonics

    Change from:
        this.btnOpen.Text = "Open";
    to:
        this.btnOpen.Text = "&Open";

    You can also do this using VS IDE designer:
    Add the Mnemonic using VS IDE designer
    Figure: Add the Mnemonic using VS IDE designer
    "Open" button does not have mnemonic (bad)
    Figure: Bad - "Open" button does not have mnemonic
    "Open" button has mnemonic (good)
    Figure: Good - "Open" button has mnemonic

    See rule Control - Do your buttons have a mnemonic?.

  9. C#/VB.NET UI & Code- OK, Cancel and Close buttons should not have mnemonics

    Change from:
        this.btnClose.Text = "&Close";
    to:
        this.btnClose.Text = "Close";

    You can also do this using VS IDE designer
    Remove the Mnemonic using VS IDE designer
    Figure: Remove the Mnemonic using VS IDE designer
    "Close" button has mnemonic (bad)
    Figure: Bad - "Close" button has mnemonic
    "Close" button does not have mnemonic (good)
    Figure: Good - "Close" button does not have mnemonic

    See rule Control - Do your buttons have a mnemonic?.

  10. C#/VB.NET UI- FixedDialog must be used with CenterParent

    Change from:
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    to:
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;

    See rule Do you use inherited forms for consistent behaviour?.

  11. C#/VB.NET UI- Winform should have its own icon instead of using the default .NET application icon

    Open the base form and add an icon:

    Add an icon to the base form
    Figure: Add an icon to the base form
    Default icon (bad)
    Figure: Bad - Winform default icon
    Your own icon (good)
    Figure: Good - Your own icon

    See rule Do you use inherited forms for consistent behaviour?.