|
-
Mar 15th, 2001, 12:19 PM
#1
Thread Starter
Member
ok, say you define a function in a .bas module
public function SayHi(Wave as boolean) as boolean
wave = true
sayhi = true
end function
when I pass the "wave" variable from a form to this module with the original value of false ( wave = false )
when the function returns,
wave is still = false!
it doesn't get modified at all through the function in the .bas module. I never had this problem with functions on the form itself, but I am trying to clean things up a bit. Anyone know how to get 'Wave' to retain the value given to it by this function residing in a module??
-
Mar 15th, 2001, 12:24 PM
#2
Addicted Member
Half-answer
I'm sorry, don't know why it doesn't. Why don't you use Public to declare a function, and store your information there? When placed the declaration in a .bas, the variable is accessable in all forms.
Hope this will help with your problem,
Quintonir
-
Mar 15th, 2001, 12:34 PM
#3
Thread Starter
Member
Me again
Perhaps it has something to do with where I am declaring the 'wave' variable??
I declare it in the general declarations of the form using:
Dim wave as boolean
could that be interfering with the .bas file?
if so
Where(how) should I declare 'Wave' ?
-Hydr0P0nix
-
Mar 15th, 2001, 12:37 PM
#4
Fanatic Member
You could make Wave global by declaring it in a code module. When you pass the value in to the function, the scope of the value is the function. Any change you make to the variable within the function is not carried outside of the function.
Or you could look into using byRef or byVal. I still haven't gotten comfortable with those, so I can't provide more detail.
-
Mar 15th, 2001, 12:39 PM
#5
Lively Member
functions
I copied your code into a module and put this code in a form. When it ran the message box displayed the value of wave as true. It worked fine, It might be in the rest of your code where the error occurs.
Private Sub Command1_Click()
Dim wave As Boolean
wave = False
If SayHi(wave) = True Then
MsgBox wave
End If
End Sub
-
Mar 15th, 2001, 12:44 PM
#6
Fanatic Member
That blows my theory away. I would have thought Wave would still be false. But I ran the code too and wave was true.
-
Mar 15th, 2001, 12:48 PM
#7
Thread Starter
Member
Weirdness
As i suspected
byref and byval are more important when you're dealing with objects, but VB uses byref by default which means when you change the value of a variable within a function, the value stays changed - it wouldn't with byval (so i read)
you were right, it was the rest of my code
here's what was going on
in the form:
Dim wave as boolean
if sayhi(cbool(wave)) then
msgbox wave
end if
in the module:
public function sayhi(wave as variant) as boolean
wave = true
end function
without the cbool you get a type mismatch.. but when i changed the form code to
Dim wave as variant
if sayhi(wave) then
msgbox wave
end if
it did indeed work..
Weirdness, thanks!
-Hydr0P0nix
-
Mar 15th, 2001, 12:52 PM
#8
Just declare it like then:
Code:
public function SayHi(ByVal Wave as boolean) as boolean
wave = true
sayhi = true
end function
-
Mar 15th, 2001, 12:56 PM
#9
Thread Starter
Member
Can't do that =)
the function is a little more complicated than that
I pass the function any number of variables
strings, integers, boolean values
looks like I have to declare everything I pass to the function as a variant unless I wanna make 3 copies of the function to handle each variable type... nahh =/
damn
-Hydr0P0nix
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|