
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Verifying Critical Parameters
|
373
// All valid types for the enum listed here.
// This means only the ones we specify are valid
// not any enum value for this enum.
// NOTE: All and VB6 enum types are not valid for this method....
case Language.CSharp:
case Language.Other:
case Language.VBNET:
break;
default:
Debug.Assert(false, language +
" is not a valid enumeration value to pass.");
throw (new
System.ComponentModel.InvalidEnumArgumentException("language",
(int)language, typeof(Language)));
break;
}
//...trailing code
}
Discussion
Testing parameters in this way does not have to be done on every method. Instead,
you should test the parameters that are passed in to all public methods of public
classes and throw an exception only if they are in error. For nonpublic methods, you
can add
Debug.Assert statements to test these parameters.
Being in control of the code within your assembly makes it much easier for you to
know which valid parameters, their ranges, and so on, you need to pass. Someone
who is unfamiliar with your assembly has a much higher chance of passing in bad
arguments to the parameters in the assembly’s public interface. Therefore, you
should guard against other developers passing bad parameters to methods in your
assembly by having both a
Debug.Assert statement checking ...