How to get a value calculated in a Function

J

J-P-W

Hi,

I have a function that looks up various values and then results in a
value to a variable:

Function ShouldItBeReferred

If Claim3Date <> "" Then
'Do stuff......

TheAnswer = "Yes"
Else
TheAnswer = "No"
End If

'and various other stuf.........

If TheAnswer = "Yes" Then
TheMessage = "Yes, for now." 'real messageto follow..
Else
TheMessage = "No, for now." 'real messageto follow..
End If

End Function

Elsewhere on the page, a number of times, I need to Call the Function
and find out the value of 'TheMessage' to display.

I've tried:

Call ShouldItBeReferred .......... nothing

DisplayMessage = ShouldItBeReferred ..........nothing

Please can you tell me how to call the function 'run it' and find out
the value of 'TheMessage'

Many thanks

Jon
 
P

Peter

Please can you tell me how to call the function 'run it' and find out
the value of 'TheMessage'


Function runit()
If condition Then
runit = "Answer 1"
Else
runit = "Answer 2"
End If
End Function

Response.Write runit

=========================================

If you intent to call this function several times with the same data being
calculated each time, why not use a sub instead ?
 
R

ravichoudhari

J-P-W said:
Hi,

I have a function that looks up various values and then results in a
value to a variable:

Function ShouldItBeReferred

If Claim3Date <> "" Then
'Do stuff......

TheAnswer = "Yes"
Else
TheAnswer = "No"
End If

'and various other stuf.........

If TheAnswer = "Yes" Then
TheMessage = "Yes, for now." 'real messageto follow..
Else
TheMessage = "No, for now." 'real messageto follow..
End If

End Function

Elsewhere on the page, a number of times, I need to Call the Function
and find out the value of 'TheMessage' to display.

I've tried:

Call ShouldItBeReferred .......... nothing

DisplayMessage = ShouldItBeReferred ..........nothing

Please can you tell me how to call the function 'run it' and find out
the value of 'TheMessage'

Many thanks

Jon


for the function to return a string value it should be defined as

function ShouldItBeReferred( paraneters-here) as string
............do something
end function

and in this function the function name is treated as a string variable
which is to be returned, so u need to assign the string values to this
ShouldItBeReferred, and it should be called as DisplayMessage =
ShouldItBeReferred(parameters-here), the DisplayMessage variable will
get the returned string value.
 
A

Anthony Jones

ravichoudhari said:
for the function to return a string value it should be defined as

function ShouldItBeReferred( paraneters-here) as string
...........do something
end function

As String cannot be used in VBScript it applies to VB6 and VB.NET. All
values in VBScript are of a variant data type which can store various
different types as is needed.
 
J

J-P-W

Peter said:
Function runit()
If condition Then
runit = "Answer 1"
Else
runit = "Answer 2"
End If
End Function

Response.Write runit

=========================================

If you intent to call this function several times with the same data being
calculated each time, why not use a sub instead ?

Thanks for the reply, I still couldn't get it to work!!! (I'd tried
this also)

I've just repeated the code within the function 4 times on the page -
sloppy I know, I'll come to it later!

Thanks

Jon
 
B

Bob Barrows [MVP]

J-P-W said:
Thanks for the reply, I still couldn't get it to work!!! (I'd tried
this also)

Please describe symptoms rather than the meaningless "doesn't work".
Verify that you have followed the above example exactly. I.E., verify
that you have assigned the value you wish the function to return to the
name of the function.
If you still can't get it to work, strip out everything that is not
needed to demonstrate the problem and show us the exact code that
"doesn't work". We need to be able to run the code ourselves, so "air"
code that somewhat resembles your code will not do it. For example, the
above example code cannot be run as-is. Here is a better example that
will work if you place it in a blank asp page:

<%
response.write runit("A")
function runit(parm)
runit = "Answer " & parm
end function
%>

This code works. I've just finished testing it. Try it out yourself and
verify that it works. If it does not work, let us know and describe the
symptoms. If it does work, check to see where the difference is in your
code. If you can't figure out the difference, post the code.
 
J

J-P-W

Bob said:
Please describe symptoms rather than the meaningless "doesn't work".
Verify that you have followed the above example exactly. I.E., verify
that you have assigned the value you wish the function to return to the
name of the function.
If you still can't get it to work, strip out everything that is not
needed to demonstrate the problem and show us the exact code that
"doesn't work". We need to be able to run the code ourselves, so "air"
code that somewhat resembles your code will not do it. For example, the
above example code cannot be run as-is. Here is a better example that
will work if you place it in a blank asp page:

<%
response.write runit("A")
function runit(parm)
runit = "Answer " & parm
end function
%>

This code works. I've just finished testing it. Try it out yourself and
verify that it works. If it does not work, let us know and describe the
symptoms. If it does work, check to see where the difference is in your
code. If you can't figure out the difference, post the code.


--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Hi Bob,

Sorry that I didn't give a full run down, I was running late on
deploying the web page that needed this function, I really appreciate
the help I get from time to time on this group, from you and others,
this time I decided that I needed to quit learning and deploy!!! I've
acutally overwritten the failing function with the repetative code that
works fine!!!

I know I should know better.....

And yes your code worked fine, thanks, it must have been the way I
transposed my data into Peters example

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top