Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use a variable as a function 1

Status
Not open for further replies.

guitardave78

Programmer
Sep 5, 2001
1,294
GB
is there a way to check if a function has been run, without haveing to run the function?

I want to send a function that returns a string, into a sub that checks id a variable is true and if it's not runs the function you have passed to it?

But i assume that if you pass the function to the sub, it runs it and just passes the string returned?

}...the bane of my life!
 
Here's some code that does what you're asking for.. but be cautious to consider whether the design is right in the first place - keep it simple if you can. There are some scenarios where this could be required, but it's best not to overcomplicate unless you have to.
Code:
<%@ Language=VBScript %>
<%
dim bRunStatus_FunA : bRunStatus_FunA = false
dim bRunStatus_FunB : bRunStatus_FunB = false

function FunA ()
  bRunStatus_FunA=true
  FunA = "Function A has been run<br />"
end function

function FunB ()
  bRunStatus_FunB=true
  FunB = "Function B has been run<br />"
end function

sub RunOnce(sFunRef)
  if Not (Eval("bRunStatus_" & sFunRef)) then
    oCmd = GetRef(sFunRef)
    response.write(oCmd)
    response.write sFunRef & " was run<br />"
  else
    response.write sFunRef & " was not run<br />"
  end if
end sub

if request.form("somevar") = "someval" then
  RunOnce ("FunA")
else
  RunOnce ("FunB")
end if

response.write("----<br />")
response.write("1: ")
Runonce("FunA")

response.write("2: ")
Runonce("FunB")

response.write("3: ")
Runonce("FunA")

response.write("4: ")
Runonce("FunB")

%>

This uses Eval and GetRef, though instead of GetRef (which gets the function pointer reference) you can also use Execute (which will execute it directly)

One limitation in the above code is it is running the function without any parameters, but you could add those in easily enough if you wanted to, in a variety of ways.
Hope that helps,


A smile is worth a thousand kind words. So smile, it's easy! :)
 
Eval, thats the one!! I knew I had used something like that before!
Thanks, that was literally all I needed!!
Code:
<%=cache.getCache("cache_competitions(0)","homepage_competitions_cache")%>
public function getCache(cacheFunc,cacheVar)
	if cache.timedOut OR application(cacheVar)="" then
		application(cacheVar) = eval(cacheFunc)
	end if
	getCache = application(cacheVar)	
end function

}...the bane of my life!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top