Here's a simple but elegant sub that I wrote to do this in a resuable way, written because I need to toggle variables between two states so often...
Code:
<%
Sub ToggleVariable(ByRef Variable, ByVal FirstValue, ByVal SecondValue)
If (Variable <> FirstValue) Then
Variable = FirstValue
Else
Variable = SecondValue
End If
End Sub
'Example usage
Dim Colour
For N = 1 To 10
Call ToggleVariable(Colour, "#FF0000", "#00FF00")
Response.Write "<font color='" & Colour & "'>" & N & "</font><br />"
Next
%>
NOTE: I always prefer the Call Sub(Param1, Param2) syntax (it's easier to read), but as always you can use the standard Sub Param1, Param2 syntax if you prefer.
This sub is also useful for toggling between True/False states, and because it modifies the variable directly (using the ByRef) it can be used for as many variables as you want at the same time (in one app I'm working on, I have 4 state variables in one loop, all toggling between states at different times). Finally, please note that because of the way that it works, you don't even need to initialise your variable with the first state value - the sub will automatically set it to the first state the first time it is called!!
Bookmarks