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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I return boolean value of a public function?

Status
Not open for further replies.

glgcag

MIS
Apr 25, 2001
160
US
I have a public function and I call it and want to determine whether the function was true of false (which I set at some point during the operation of the function).

Here's the function in general terms:

Code:
Public Function AttachEmail(byVal sFolder as string) as Boolean

   AttachEmail = True

End Function

In my code I want to call this function and then in the next line determine if the function is true or false. How do I do it? I tried:

If AttachEmail = False then yada yada

But I just got an error.

I appreciate the help!
 
According to the declaration of the function, it should probably be:

[tt]dim s as string
s="some valid path"
If not AttachEmail(s) then yada yada[/tt]

Roy-Vidar
 
Roy-Vidar,

It worked, thank you. But just to understand a little better, if I set the function to false and then put

If Not AttachEmail(s) then . . .

the code tests for AttachEmail = False in the function?

So if I wanted to test for AttachEmail = true in the function I would write

If AttachEmail(s) then . . .

right?
 
I don't understand what you're asking.

You have specified the return value of the function to be of the type Boolean (-1/0 - True/False). The function will return whatever value you assign it (or if you forget to assign, I believe it will return false).

The line
[tt]AttachEmail = True[/tt]

within the function is not testing anything, it is assigning the value of True (-1) as the return value of the function.

As for testing with:

[tt]if myBoolean = True then[/tt]

vs

[tt]if myBoolean then[/tt]

They do exactly the same, in the first one you would have either:

[tt]if True = True then ' or
if False = True then[/tt]

- which for some increases the readability, but in my view (preference, habit...) is redundant. The if statement requires one boolean value to evaluate, why have it compare two booleans to get the one? Say you want to toggle the enabled property of a form control based on the return value from a function. Using the "full syntax" would require:

[tt]if myfunction() = true then
me!mycontrol.enabled = true
else
me!mycontrol.enabled = false
end if[/tt]

Since the function returns either true or false, why not:

[tt]me!mycontrol.enabled=myfunction()[/tt]

- saves typing;-) - but it's also faster (more important).

Does this answer any of the questions?

Roy-Vidar
 
Yes, this answers my question. I guess where I was getting a little confused is the way I was coding. Here's what I did wrong:

Code:
Call AttachEmail(s)

If AttachEmail = True then . . .

Whereas, what I should have done is:

Code:
If AttachEmail(s) = True then . . .

Also, I completely understand the example you give of making a control equal to the result of the boolean. That really is less typing and if it's faster I'll definitely use it now that I understand how to actually get the value of a function that is boolean.

Thanks again for the help, you really cleared things up for me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top