crosspost from http://rex.la/blogs/work/
It’s mostly required to do the initial values validation while an application start, no matter it’s a winform or webform, a page taking values from query string or a web control taking values from attributes.
It’s common for page to take parameters from query string during post backs, also normal for web user control to get parameters from attributes specified from page or other controls. It would be a good practice to always validate those values before start processing logics.
for parameters there will always some values (mostly string values) pre-defined for user to specify. normally some integers, or strings like “true” or “false” or others custimized. by doing a initial value validation while page_init make sure the control gor the correct value and reduce the error handling inside logics. normally the validation code would like the following:
if (strValue1 == null) strValue1 = “”; // be sure always check null.
if (strValue1.ToLower() != “true” && strValue1.ToLower() != “false”) strValue1 = “false”;
if (strLevel == null) strLevel = “”;
if (strLevel.ToLower() != “low” && strLevel.ToLower() != “middle” && strLevel.ToLower() != “high”) strLevel = “middle”;
this will get little lousy if a parameter got too many options to set.
null is always the thing needs to be checked, since one may not able to know if certain variable was initialized or not. make sure at least the string is not null, then for those lousy if statement, just found a way to reduce it, by using string comparison:
if (strLevel == null) strLevel = “”; // make sure it’s not null to prevent runtime error
if (“_low,middle,high”.IndexOf(strLevel.ToLower()) < 1) strLevel = “middle”;
if (strValue2 == null) strValue2 = “”;
if (“_1_2_3_4_5_6_”.IndexOf(strValue2) < 1) strValue2 = “1”; // check if the value is 1~6.
using IndexOf to check the occurence of preferred values, reduce the if (…) …; code needed. some notices of this are:
strLevel can not be null or it will cause run-time error. so check the null first.
Astring.IndexOf(“”) always return 0, which make sense anyway. this will be the same index value of first char of the string to be compared. therefore do not put comparing string from first position. instead, putting them started from the second char by putting a space or anything in the first char. and do the compared value that’s ” < 1 ” instead of ” < 0″ .
for the concern of matching the wrong value wanted, eg intened to matching “1” inside the string “_123456″ , but a value “12” comes in, it will match ( index at 1 ) but still got the wrong value required. put seperators between matching values should solve this (eg “_1_2_3_4_5_6_”).
RegularExpression class should be the right one to choose for this operation, but it still seems too lousy to me.
this should reduce some amount of coding, also easy to understand the intension. and page or control will got correct and required values while processing logics thus reduce validation code needed.
Technorati Tags: asp.net , programming