Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Functions within Functions
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Functions within Functions

 Post Reply Post Reply
Author
ub3rl337ch3ch View Drop Down
Senior Member
Senior Member
Avatar

Joined: 16 February 2005
Location: Australia
Status: Offline
Points: 341
Post Options Post Options   Thanks (0) Thanks(0)   Quote ub3rl337ch3ch Quote  Post ReplyReply Direct Link To This Post Topic: Functions within Functions
    Posted: 27 June 2005 at 10:04pm
I have the following two functions, the checknull one being called repeatedly in the blah() one (obviously the entire blah function isn't there) . If FarmName has length 0, the output from it all is TrueFalseTrue.
 
Basically, it sets valid as False, and writes it as such within the function, but then when it leaves the chenull function to go back to the main part of the blah function, valid is not longer False and is treated as though it had never changed.
 
Is this because I am using one function within another? Any other suggestions which might shed some light on this? thx
 
 
 
function checknull(theString,validvar)
theString = Trim(theString) validvar = "True"
if Len(theString) = 0 then
valid = "False"
validvar = "False"
end if
Response.Write valid
end function
 
 
function blah()
valid = "True"
call checknull(FarmName,vFarmName)
Response.Write valid
end function
Back to Top
ub3rl337ch3ch View Drop Down
Senior Member
Senior Member
Avatar

Joined: 16 February 2005
Location: Australia
Status: Offline
Points: 341
Post Options Post Options   Thanks (0) Thanks(0)   Quote ub3rl337ch3ch Quote  Post ReplyReply Direct Link To This Post Posted: 27 June 2005 at 10:13pm
I suppose I should add that I have never had a problem with the valid variable's value carrying through from the checknull function, but I have never tried to use it as a part of another function before.
Back to Top
dpyers View Drop Down
Senior Member
Senior Member


Joined: 12 May 2003
Status: Offline
Points: 3937
Post Options Post Options   Thanks (0) Thanks(0)   Quote dpyers Quote  Post ReplyReply Direct Link To This Post Posted: 28 June 2005 at 5:41am
Dim valid before the functions. Your problem is that the scope of the variable is restricted to within the function.  Also use By Val for the parameters. Valid may also be a reserved word.

Technically what you have coded are sub's, not functions. A function should only return a value of true/false.

I would have done it something like...

Dim strTheString
Dim strValid
strTheString = "xxxxx"

Function fncCheckNull (By Val strTheString)
'True if strTheString contains non-blank characters
   If Len(Trim(strTheString) > 0 Then
      fncCheckNull = True
   Else
      fncCheckNull = False
   End If
End Function

Sub subBlah()
   If fncCheckNull(strTheString) Then
      strValid = "True"
   Else
      strValid = "False"
   End If
   Response.Write strValid
End Sub

  

Lead me not into temptation... I know the short cut, follow me.
Back to Top
ub3rl337ch3ch View Drop Down
Senior Member
Senior Member
Avatar

Joined: 16 February 2005
Location: Australia
Status: Offline
Points: 341
Post Options Post Options   Thanks (0) Thanks(0)   Quote ub3rl337ch3ch Quote  Post ReplyReply Direct Link To This Post Posted: 28 June 2005 at 6:34pm
what does By Val do? I've never encountered that before.
Back to Top
dpyers View Drop Down
Senior Member
Senior Member


Joined: 12 May 2003
Status: Offline
Points: 3937
Post Options Post Options   Thanks (0) Thanks(0)   Quote dpyers Quote  Post ReplyReply Direct Link To This Post Posted: 28 June 2005 at 6:44pm
Actually, I think I should have said By Ref instead of By Val.

By Val means the value within a string/whatever is passed to the function in a separate memory space. Changing the value in a function does not affect the original string.
By Ref means to pass a reference to the location of the string. Changing the value in a function changes the original source.

Lead me not into temptation... I know the short cut, follow me.
Back to Top
ub3rl337ch3ch View Drop Down
Senior Member
Senior Member
Avatar

Joined: 16 February 2005
Location: Australia
Status: Offline
Points: 341
Post Options Post Options   Thanks (0) Thanks(0)   Quote ub3rl337ch3ch Quote  Post ReplyReply Direct Link To This Post Posted: 28 June 2005 at 6:47pm
great, thanks.
 
Do you have any idea why it would be working with only the single function, but not that same function within another? is it something asp isnt capable of? or should it be possible to have functions/subs within other functions/subs?
Back to Top
dpyers View Drop Down
Senior Member
Senior Member


Joined: 12 May 2003
Status: Offline
Points: 3937
Post Options Post Options   Thanks (0) Thanks(0)   Quote dpyers Quote  Post ReplyReply Direct Link To This Post Posted: 28 June 2005 at 7:19pm
I think if you Dim the variable before calling either function and use By Ref, changing the contents wherever will stick.

Here's an article from my bookmarks That explainms things better than me.
http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=33

Lead me not into temptation... I know the short cut, follow me.
Back to Top
ub3rl337ch3ch View Drop Down
Senior Member
Senior Member
Avatar

Joined: 16 February 2005
Location: Australia
Status: Offline
Points: 341
Post Options Post Options   Thanks (0) Thanks(0)   Quote ub3rl337ch3ch Quote  Post ReplyReply Direct Link To This Post Posted: 28 June 2005 at 7:23pm
cheers
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.