9 Dec
2004

Refactoring Isa to Hasa

So the natural evolution of a few of my last posts is to put all of the
functions together from my base class into a single usable class all it’s own.

Aggregating these things into a single class makes the functions portable
between web apps so that these things do not need to be rewritten in the base Page
class of every app that you write.

function ToggleSourceCodeRegion(regionNumber)
{
var divRegion = document.getElementById(‘region’ + regionNumber);
var divRegionBlock = document.getElementById(‘regionBlock’ + regionNumber);

if (divRegion.style.display == ‘inline’)
{
divRegion.style.display = ‘none’;
divRegionBlock.style.display = ‘inline’;
}
else
{
divRegion.style.display = ‘inline’;
divRegionBlock.style.display = ‘none’;
}
}

I am curious about your opinions weighing “Has a“ versus
“Is a“ models for these functions.

1public class ComponentUtil 2{ 3 public static void AddClientSideConfirmation(
WebControl wc, string confirmationMessage ) 4 { 5 wc.Attributes["onclick"]
= "return confirm('" + confirmationMessage + "');"; 6 } 7 8 public static void ThrowClientSideError(
Page page, string errorMessage ) 9 { 10 string err
= "<script language='JavaScript'>alert('" +
errorMessage + "');</script>"; 11 12 if (
! page.IsStartupScriptRegistered( "clientScript" )
) 13 { 14 page.RegisterStartupScript( "clientScript",
err ); 15 } 16 } 17 18 public static string ParseRequiredParam(
Page page, string paramName ) 19 { 20 string value
= page.Request.Params[ paramName ]; 21 if(
value == null || value.Length < 1 ) 22 { 23 throw new ArgumentNullException(
paramName, "Parameter is required."); 24 } 25 return value; 26 } 27 28 public static string ParseOptionalParam(
Page page, string paramName ) 29 { 30 return ParseOptionalParam(
page, paramName, null ); 31 } 32 33 public static string ParseOptionalParam(
Page page, string paramName, string defaultValue
) 34 { 35 string value
= page.Request.Params[ paramName ]; 36 if(
value == null || value.Length < 1 ) 37 { 38 value
= defaultValue; 39 } 40 return value; 41 } 42}