Results 1 to 9 of 9

Thread: Relatively Easy Question

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    57

    Cool

    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??

  2. #2
    Addicted Member Quintonir's Avatar
    Join Date
    Mar 2001
    Location
    The Netherlands
    Posts
    155

    Exclamation 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

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    57

    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

  4. #4
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    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.

  5. #5
    Lively Member Optic's Avatar
    Join Date
    Mar 2001
    Location
    I'm everywhere and nowhere, I'm inside your computer and inside your mind. You can't escape me for I am vision.
    Posts
    117

    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

  6. #6
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    That blows my theory away. I would have thought Wave would still be false. But I ran the code too and wave was true.

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    57

    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

  8. #8
    Guest
    Just declare it like then:
    Code:
    public function SayHi(ByVal Wave as boolean) as boolean 
        wave = true 
        sayhi = true 
    end function

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    57

    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
  •  



Click Here to Expand Forum to Full Width