<?xml version="1.0" encoding="iso-8859-1"?>
<feed version="0.3"
  xmlns="http://purl.org/atom/ns#"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xml:lang="en">
	<title>Marcus's Musings</title>
	<link rel="alternate" type="text/html" href="http://blog.marcustucker.com/" />
	<tagline>THOUGHTS, TIPS, AND LINKS COVERING WEB DEVELOPMENT AND RELATED TOPICS</tagline>
	
	<modified>2005-06-02T14:16:35+01:00</modified>
	<copyright>Copyright 2004-2005</copyright>
	<generator url="http://www.uapplication.com/" version="Ublog Reload 1.0.5">Ublog Reload 1.0.5</generator>

	<entry>
	  	<author>
			<name>Marcus</name>
			<email>me@marcustucker.com</email>
		</author>
		<title><![CDATA[Code Snippets (I)]]></title>
		<link rel="alternate" type="text/html" href="http://blog.marcustucker.com/blog_comment.asp?bi=68" />
		<id>http://blog.marcustucker.com/blog_comment.asp?bi=68</id>
		<modified>2005-02-02T13:55:02+01:00</modified>
		<issued>2005-02-02T13:55:02+01:00</issued>
		<created>2005-02-02T13:55:02+01:00</created>
		<content type="text/html" mode="escaped" xml:base="http://blog.marcustucker.com/blog_comment.asp?bi=68"><![CDATA[A few simple but handy snippets of code for you...<br /><br /><b>Kill</b><br />Firstly, since I use it in almost every function which uses objects, I thought I'd better repost Kill(), which tidies up objects and arrays, with special handling for ADO and Dictionary objects.<br /><br /><b>NOTE: All code posted on this blog from now on will assume that this function is part of your includes</b><br /><br /><pre>Function Kill(byref Obj)<br />	Select Case True<br />		Case IsObject(Obj)<br />			Select Case LCase(TypeName(Obj))<br />				Case "recordset", "command", "stream", "connection"<br />					'ADO objects<br />					If Obj.State <> 0 then<br />						Obj.Close<br />					End If<br />				<br />				case "dictionary"<br />					'remove all the pairs<br />					Obj.RemoveAll<br />					<br />				Case else<br />					'something else so don't<br />					'do anything special<br />					<br />			End Select<br />			<br />			Set Obj = Nothing<br />		<br />		Case IsArray(Obj)<br />			'clear the array<br />			Erase Obj<br />		<br />		Case Else<br />			'do nothing at all<br />			<br />	End Select<br />		<br />	'Now revert it to an unitialized state<br />	Obj = Empty<br />End Function</pre><br /><br /><br /><b>IsBlank</b><br />This is another of my most frequently used snippets, which acts as a catch-all checker for variables, meaning that you no longer have to worry about the variable type when you want to use a <b>If MyVar="" Then</b> statement. It will automatically test the variable using all the appropriate checks - see the code for exactly what it does and how. Simple, but remarkably handy! Give it a go next time you write a complicated script or entire app!<br /><br /><b>NOTE: All code posted on this blog from now on will assume that this function is part of your includes</b><br /><br /><pre>Function IsBlank(ByRef Var)<br />	IsBlank = False<br />	<br />	Select Case True<br />		Case IsObject(Var)<br />			If var Is Nothing Then<br />				IsBlank = True<br />			End If<br />		<br />		Case IsEmpty(Var), IsNull(Var)<br />			IsBlank = True<br />			<br />		Case IsArray(Var)<br />			If UBound(Var) = 0 Then<br />				IsBlank = True<br />			End If<br />		<br />		Case IsNumeric(Var)<br />			If (Var = 0) Then<br />				IsBlank = True<br />			End If<br />			<br />		Case Else<br />			If Trim(Var) = "" Then<br />				IsBlank = True<br />			End If<br />	End Select<br />End Function</pre><br /><br /><br /><b>ToggleVariable</b><br />Invaluable for all sorts of situations where variable needs to flip-flop between two values - one example being alternating colours for table rows.<br /><br /><pre>'Toggles a variable between two values, and will initalise<br />'the variable to the "on" state if it doesn't have either value<br />Function ToggleVariable(Byref Variable, ByVal StateOn, ByVal StateOff)<br />	If (Variable = StateOn) Then<br />		Variable = StateOff<br />	Else<br />		Variable = StateOn<br />	End If<br /><br />	ToggleVariable = Variable<br />End Function</pre>For example:<pre>For N = 1 To 10<br />	Response.Write "<" &amp; "tr bgcolor=""" &amp; ToggleVariable(C, "#ffaaaa", "#aaffaa") &amp; """>"<br />	Response.Write "<" &amp; "td>" &amp; N &amp; "<" &amp; "/td>"<br />	Response.Write "<" &amp; "/tr>"<br />Next</pre>(tags are broken up to circumvent posting problems in this blog)<br /><br />Note that although it's a function which returns the new value, it does directly modify the variable (because it's passed ByRef) and therefore can be called as a sub too:<br /><pre>For N = 1 To 10<br />	Call ToggleVariable(OddRowIndicator, True, False)<br />	If (OddRowIndicator = True) Then<br />		'do something here<br />	End If<br />Next</pre><br /><br /><br /><b>GetCurrentURL</b><br />Sometimes it's handy to know what the current URL is within your script, but strangely enough ASP doesn't expose this information directly, instead it must be assembled from various server variables. Here's a little function which does it for you:<br /><pre>Function GetCurrentURL()<br />	'Select the protocol<br />	If request.servervariables("HTTPS") = "on" Then<br />		GetCurrentURL = "https://"<br />	Else<br />		GetCurrentURL = "http://"<br />	End If<br />	<br />	GetCurrentURL = GetCurrentURL &amp; Request.ServerVariables("SERVER_NAME")<br />	<br />	If (Request.ServerVariables("SERVER_PORT") <> 80) Then<br />		GetCurrentURL = GetCurrentURL &amp; ":" &amp; Request.ServerVariables("SERVER_PORT")<br />	End If<br />	<br />	GetCurrentURL = GetCurrentURL &amp; Request.ServerVariables("URL")<br />	<br />	If (Request.QueryString <> "") Then<br />		GetCurrentURL = GetCurrentURL &amp; "?" &amp; Request.QueryString<br />	End If<br />End Function</pre><br /><br /><br /><b>IIf</b><br />And last but not least here's, a handy VBScript version of a VB statement which is also commonly used as a macro in C/C++. This snippet lets you condense a simple "If...Then...Else...End If" block into a single line.<br /><br /><pre>'Immediate If<br />Function IIf(Condition, ValueIfTrue, ValueIfFalse)<br />	If Condition Then<br />		IIf = ValueIfTrue<br />	Else<br />		IIf = ValueIfFalse<br />	End if<br />End Function</pre><br /><br />For example, you could turn this:<br /><pre>If IsNull(Price) Then<br />	Response.Write "(n/a)"<br />Else<br />	Response.Write Price<br />End If</pre><br />Into this:<br /><pre>Response.Write IIF(IsNull(Price), "(n/a)", Price)</pre>However, let me just make you aware of a slight caveat that presents itself when using this shorthand form...<br /><br />Let's say that we wanted to format the price as a number with 2 decimal places - one might naturally use <b>FormatNumber(Price, 2)</b> to do this and insert it into the code like so:<pre>Response.Write IIF(IsNull(Price), "(n/a)", FormatNumber(Price, 2))</pre>Unfortunately, this won't work. When you call a function or sub, all parameters are passed either ByRef or ByVal, but regardless it is either a variable (which may contain an object, array, or value) or a value which is passed. Therefore, all three parameters to the IIF function are evaluated before the function is called, and the resulting values are passed.<br /><br />The consequence of this is that if the variable <b>Price</b> is ever <b>Null</b>, the script will throw an error, because <b>FormatNumber()</b> chokes on <b>Nulls</b>. Therefore, be careful about how you use this and remember that is not quite a 1:1 replacement for the full statement block.<br /><br /><br />That's all for now, if you have any questions please post a comment and I'll be sure to answer it.]]></content>
	</entry>

</feed>