Results 1 to 6 of 6

Thread: About ByRef

  1. #1

    Thread Starter
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577

    Question About ByRef

    Hi, im really confused and i cant work out why this does this.

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim a As Long
    3.     a = 10
    4.     c = f(a)
    5.     MsgBox a
    6. End Sub
    7.  
    8. Private Function f(b As Long) As Long
    9.     b = b + 1
    10. End Function

    Can someone please explain why a = 11 after this code is done?
    i thought that you had to use ByRef keyword if you wanted to change the original value??
    ie
    VB Code:
    1. Private Function f(ByRef b as long) as long

    How is it possible that 'a' variable changes when all im doing is passing the 'a' value to the function?

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    ByRef is ByDefault.

  3. #3

    Thread Starter
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    Ok thats totally disturbed me now, from 3 years of college to learning Visual Basic i always thought ByVal was default and ive always worked as if it was.
    How i never ran into a problem before today i totally dont know

  4. #4
    Hyperactive Member Jason Badon's Avatar
    Join Date
    Feb 2001
    Location
    Colorado
    Posts
    329
    I think this might help

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim a As Long
    3.     a = 10
    4.     c = f(a)
    5.     MsgBox a
    6. End Sub
    7.  
    8. Private Function f(b As Long) As Long
    9.     ' At this point a = b and b = a so when you add 1 to b you are adding 1 to a
    10.     b = b + 1
    11. End Function


    Looking at what you had this looks like what you were tiring to accomplish.
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim a As Long
    3.     a = 10
    4.     c = f(a)
    5.     MsgBox a
    6.     MsgBox f(a)
    7.     MsgBox c
    8. End Sub
    9.  
    10. Private Function f(b As Long) As Long
    11.     f = b + 1
    12. End Function

  5. #5

    Thread Starter
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    Thanks but i solved it by

    VB Code:
    1. Private Function f( ByVal b as long) as long
    2. b=b+1
    3. End Function

    that keeps the original variable 'a' as 10

    its just that i always thought ByVal was default and im suprised that ive never had any problems as i always thought it was ByVal

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Thou art doomed.

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