Blog homepage

Content from old blog

www.marcustucker.com

Bookmark this page



previous month  SEPTEMBER 2010  next month
s m t w t f s
1 2 3 4
5 6 7 8 9
10
11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30


BLOG ARCHIVE
 
RSS ATOM  Full archive
 
current month



CATEGORIES
 
alert [ 1 ]  RSS ATOM
blog [ 8 ]  RSS ATOM
development [ 9 ]  RSS ATOM
    ASP / scripting  [ 13RSS ATOM
        snippets [ 1RSS ATOM
    database [ 0 ] RSS ATOM
    web standards  [ 1RSS ATOM
    XML [ 0 ] RSS ATOM
general [ 24 ]  RSS ATOM
    humour [ 0 ] RSS ATOM
personal [ 6 ]  RSS ATOM
photography [ 1 ]  RSS ATOM
politics [ 1 ]  RSS ATOM
rant [ 8 ]  RSS ATOM
reading [ 18 ]  RSS ATOM


SEARCH
 


BLOGROLL
 
 


ARTICLES
 
Access Stored Queries

VBScript String Concatenation
 


2 Feb 2005
Code Snippets (I)
A few simple but handy snippets of code for you...

Kill
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.

NOTE: All code posted on this blog from now on will assume that this function is part of your includes

Function Kill(byref Obj)
Select Case True
Case IsObject(Obj)
Select Case LCase(TypeName(Obj))
Case "recordset", "command", "stream", "connection"
'ADO objects
If Obj.State <> 0 then
Obj.Close
End If

case "dictionary"
'remove all the pairs
Obj.RemoveAll

Case else
'something else so don't
'do anything special

End Select

Set Obj = Nothing

Case IsArray(Obj)
'clear the array
Erase Obj

Case Else
'do nothing at all

End Select

'Now revert it to an unitialized state
Obj = Empty
End Function



IsBlank
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 If MyVar="" Then 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!

NOTE: All code posted on this blog from now on will assume that this function is part of your includes

Function IsBlank(ByRef Var)
IsBlank = False

Select Case True
Case IsObject(Var)
If var Is Nothing Then
IsBlank = True
End If

Case IsEmpty(Var), IsNull(Var)
IsBlank = True

Case IsArray(Var)
If UBound(Var) = 0 Then
IsBlank = True
End If

Case IsNumeric(Var)
If (Var = 0) Then
IsBlank = True
End If

Case Else
If Trim(Var) = "" Then
IsBlank = True
End If
End Select
End Function



ToggleVariable
Invaluable for all sorts of situations where variable needs to flip-flop between two values - one example being alternating colours for table rows.

'Toggles a variable between two values, and will initalise
'the variable to the "on" state if it doesn't have either value
Function ToggleVariable(Byref Variable, ByVal StateOn, ByVal StateOff)
If (Variable = StateOn) Then
Variable = StateOff
Else
Variable = StateOn
End If

ToggleVariable = Variable
End Function
For example:
For N = 1 To 10
Response.Write "<" & "tr bgcolor=""" & ToggleVariable(C, "#ffaaaa", "#aaffaa") & """>"
Response.Write "<" & "td>" & N & "<" & "/td>"
Response.Write "<" & "/tr>"
Next
(tags are broken up to circumvent posting problems in this blog)

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:
For N = 1 To 10
Call ToggleVariable(OddRowIndicator, True, False)
If (OddRowIndicator = True) Then
'do something here
End If
Next



GetCurrentURL
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:
Function GetCurrentURL()
'Select the protocol
If request.servervariables("HTTPS") = "on" Then
GetCurrentURL = "https://"
Else
GetCurrentURL = "http://"
End If

GetCurrentURL = GetCurrentURL & Request.ServerVariables("SERVER_NAME")

If (Request.ServerVariables("SERVER_PORT") <> 80) Then
GetCurrentURL = GetCurrentURL & ":" & Request.ServerVariables("SERVER_PORT")
End If

GetCurrentURL = GetCurrentURL & Request.ServerVariables("URL")

If (Request.QueryString <> "") Then
GetCurrentURL = GetCurrentURL & "?" & Request.QueryString
End If
End Function



IIf
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.

'Immediate If
Function IIf(Condition, ValueIfTrue, ValueIfFalse)
If Condition Then
IIf = ValueIfTrue
Else
IIf = ValueIfFalse
End if
End Function


For example, you could turn this:
If IsNull(Price) Then
Response.Write "(n/a)"
Else
Response.Write Price
End If

Into this:
Response.Write IIF(IsNull(Price), "(n/a)", Price)
However, let me just make you aware of a slight caveat that presents itself when using this shorthand form...

Let's say that we wanted to format the price as a number with 2 decimal places - one might naturally use FormatNumber(Price, 2) to do this and insert it into the code like so:
Response.Write IIF(IsNull(Price), "(n/a)", FormatNumber(Price, 2))
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.

The consequence of this is that if the variable Price is ever Null, the script will throw an error, because FormatNumber() chokes on Nulls. Therefore, be careful about how you use this and remember that is not quite a 1:1 replacement for the full statement block.


That's all for now, if you have any questions please post a comment and I'll be sure to answer it.
 
ASP / scripting , development , snippets
posted by  Marcus at  13:55 | permalink | comments [3] | trackbacks [32]