VB.NET supplies the "Optional" keyword for parameters in methods, if none is supplied you can explicitely define a default value in the parameter signature. C# doesn't supply this functionality (and for good reason, it leads to lazy coding). it's good coding practice to always check the values of parameters in your components, and either throw an assertion or an exception if it's not supplied, like so:
void myMethod(type myType1, type myType2...)
{
// check here for params that cannot be null, or "" for strings
if(myType == null) throw new ArgumentNullException([message], [inner exception]...);
// now check for validity of the param using regex, etc, you can't count on client code doing these checks
if(myType != [some sort of criteria]) throw new ArgumentException ([message], [inner exception]...);
}that being said, if you wanted to provide a default value, you can use ternary syntax to provide one
void myMethod(type myType1, type myType2...)
{
myType1 != null ? MyType1 : [your default value here];
}of course if you want to allow null values, you won't need to test for param values. if you follow these guidelines, you will insure that your components get used in the correct way.
Tip number 2:
ALWAYS ALWAYS ALWAYS call either string.Trim().ToLower() or string.Trim().ToUpper if you are using/comparing string params in C#...i spent the better part of 2 hours trying to track down a subtle bug, and it turned out to be that i was comparing strings that were spelled the same, however their ASCII values were different. VB.NET of course doesn't care about case, but it's good coding style nonetheless.
happy coding :-)
j



No comments yet