Click here to Skip to main content
Licence CPOL
First Posted 20 Mar 2009
Views 7,956
Downloads 44
Bookmarked 11 times

Argument Helper

By | 20 Mar 2009 | Article
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:

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.

/// <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)

About the Author

Andrew_Thomas

Chief Technology Officer
Intuitive Search Technologies
United Kingdom United Kingdom

Member

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...

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
General[My vote of 2] Do better PinmemberDonsw17:13 19 Apr '09  
GeneralRe: [My vote of 2] Do better PinmemberAndrew_Thomas23:28 19 Apr '09  
GeneralRe: [My vote of 2] Do better PinmemberDonsw1:36 20 Apr '09  
GeneralMy vote of 1 PinmemberShukaido9:25 31 Mar '09  
GeneralRe: My vote of 1 PinmemberAndrew_Thomas0:09 20 Apr '09  
GeneralMy vote of 1 Pinmember_FleX13:53 20 Mar '09  
GeneralRe: My vote of 1 Pinmemberdimzon2:01 21 Mar '09  
GeneralRe: My vote of 1 PinmemberAndrew_Thomas23:03 22 Mar '09  
GeneralRe: My vote of 1 Pinmembergonalo22:55 23 Mar '09  
GeneralRe: My vote of 1 [modified] PinmemberTimMerksem6:24 24 Mar '09  
GeneralRe: My vote of 1 PinmemberAndrew_Thomas11:49 24 Mar '09  
GeneralRe: My vote of 1 Pinmemberptmcomp10:20 24 Mar '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 20 Mar 2009
Article Copyright 2009 by Andrew_Thomas
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid