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) 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) 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 initaliseFor example: For N = 1 To 10(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 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() 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 For example, you could turn this: If IsNull(Price) Then 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] | |

