Click here to Skip to main content
15,867,771 members
Articles / Programming Languages / C#

Argument Helper

Rate me:
Please Sign up or sign in to vote.
2.80/5 (5 votes)
20 Mar 2009CPOL 24.7K   119   11   12
An argument helper for checking arguments passed into methods.

Introduction

This code example shows how to make your public methods safer to use. Especially, if they are to be used as libraries by others who don't have access to the source code.

Background

I thought I would share some functionality to help argument checking for public methods/properties. All public/internal methods in assemblies should have appropriate argument checking and throw the appropriate exceptions:

  • ArgumentNullException – A null was passed when an argument must be specified.
  • ArgumentException – An argument passed was invalid for a certain reason.

Using the code

Obviously, the exceptions should only be thrown where appropriate, but if the code within your method requires the arguments to be in a certain state, you need to ensure it. To this end, I have added some helpers:

C#
ArgumentHelper.AssertEnumMember<T>(enumArgument);
ArgumentHelper.AssertEnumMember<T>(enumArgument, enumValuesValid[]);
ArgumentHelper.AssertNotNull<T>(notNullArgument, " notNullArgument ");
ArgumentHelper.AssertNotEmptyAndNotNull(notEmptyAndNotNullArgument, 
               "notEmptyAndNotNullArgument");

They are pretty self explanatory, but are useful for throwing for simpler checking.

C#
/// <summary>
/// Retrieves all available plugins based on plugin type.
/// </summary>
/// <param name="pluginType">Type of plugins to get.</param>
/// <param name="plugins">List to populate.</param>
/// <param name="pluginFile">Some plugin file.</param>
/// <param name="canBeNull">Can be null string.</param>
/// <returns>True if successful.</returns>
public static bool GetAllPlugins(PluginType pluginType, IList<string> plugins, 
                                 string pluginFile, string canBeNull)
{
    ArgumentHelper.AssertEnumMember<PluginType>(pluginType, 
                   new PluginType[] {PluginType.Global, PluginType.Packager}); 
    ArgumentHelper.AssertNotNull<IList<string>>(plugins, "plugins");
    ArgumentHelper.AssertNotEmptyAndNotNull(pluginFile, "pluginFile"); 

    if (canBeNull == null) //can be null so need need to check above.
      return false;
}

History

  • 20/3/2009 - Uploaded.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Intuitive Search Technologies
United Kingdom United Kingdom
Originally from New Zealand, currently work as Development Directory at a software company in the UK specialising in online marketing and advertising.
I have a blog located at: http://andrew.thomas.net.nz, which is all about development in Microsoft .Net, focused on C#, Asp .NET, SQL Server and SEO. Check it out...

Comments and Discussions

 
General[My vote of 2] Do better Pin
Donsw19-Apr-09 17:13
Donsw19-Apr-09 17:13 
GeneralRe: [My vote of 2] Do better Pin
Andrew_Thomas19-Apr-09 23:28
Andrew_Thomas19-Apr-09 23:28 
GeneralRe: [My vote of 2] Do better Pin
Donsw20-Apr-09 1:36
Donsw20-Apr-09 1:36 
GeneralMy vote of 1 Pin
Shukaido31-Mar-09 9:25
Shukaido31-Mar-09 9:25 
GeneralRe: My vote of 1 Pin
Andrew_Thomas20-Apr-09 0:09
Andrew_Thomas20-Apr-09 0:09 
GeneralMy vote of 1 Pin
Bad code hunter20-Mar-09 13:53
Bad code hunter20-Mar-09 13:53 
GeneralRe: My vote of 1 Pin
dimzon21-Mar-09 2:01
dimzon21-Mar-09 2:01 
GeneralRe: My vote of 1 Pin
Andrew_Thomas22-Mar-09 23:03
Andrew_Thomas22-Mar-09 23:03 
GeneralRe: My vote of 1 Pin
gonalo23-Mar-09 22:55
gonalo23-Mar-09 22:55 
"It is absurd to make elaborate security checks on debugging runs, when no trust
is put in the results, and then remove them in production runs, when an erroneous
result could be expensive or disastrous. What would we think of a sailing
enthusiast who wears his life-jacket when training on dry land but takes it off as soon as he goes to sea?"

From Hoare, 1973.

hola

GeneralRe: My vote of 1 [modified] Pin
TimMerksem24-Mar-09 6:24
TimMerksem24-Mar-09 6:24 
GeneralRe: My vote of 1 Pin
Andrew_Thomas24-Mar-09 11:49
Andrew_Thomas24-Mar-09 11:49 
GeneralRe: My vote of 1 Pin
ptmcomp24-Mar-09 10:20
ptmcomp24-Mar-09 10:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.