CFLib.org – Common Function Library Project

ToggleValue(variable, value1, value2)

Last updated July 3, 2002

author

Nathan Dintenfass

Version: 1 | Requires: CF5 | Library: UtilityLib

Description:
Save yourself a couple lines of coding next time you need to toggle the value of a variable.

Return Values:
Returns a string.

Example:

<cfset action = "On">
<cfset newValue = toggleValue(action,"On","Off")>
<cfoutput>Action = #action#, toggle is #toggleValue(action,"On","Off")#</cfoutput>

Parameters:

Name Description Required
variable The variable that stores the value you will toggle. Yes
value1 The first value of the toggle. Yes
value2 The second value of the toggle. Yes

Full UDF Source:

/**
 * Toggles a value (ie: &quot;stop&quot;/&quot;start&quot;) between two options.
 * 
 * @param variable 	 The variable that stores the value you will toggle. (Required)
 * @param value1 	 The first value of the toggle. (Required)
 * @param value2 	 The second value of the toggle. (Required)
 * @return Returns a string. 
 * @author Nathan Dintenfass (nathan@changemedia.com) 
 * @version 1, July 3, 2002 
 */
function toggleValue(variable,value1,value2){
	//make a struct in which the value is the opposite of the key
	var toggler = structNew();
	toggler[value1] = value2;
	toggler[value2] = value1;
	//return whichever value is not the current value
	return toggler[variable];
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson