Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. pass by reference is default?

pass by reference is default?

Scheduled Pinned Locked Moved Visual Basic
questionhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    The help file says VB uses pass-by-reference as a default(byval to use pass by value) - but the simple test I do in an Excel module isn't working Public testnum ----- Public Sub makebig(inpt) MsgBox "Make big receives = " & inpt inpt = inpt + 1 MsgBox "Input now is = " & inpt MsgBox "testnum now is = " & testnum End Sub --- Public Sub test() testnum = 1 MsgBox "Before = " & testnum makebig (testnum) MsgBox "After = " & testnum End Sub test() does not change testnum, Before = 1, After = 1 what is happening? Thanks heaps

    K L 2 Replies Last reply
    0
    • L Lost User

      The help file says VB uses pass-by-reference as a default(byval to use pass by value) - but the simple test I do in an Excel module isn't working Public testnum ----- Public Sub makebig(inpt) MsgBox "Make big receives = " & inpt inpt = inpt + 1 MsgBox "Input now is = " & inpt MsgBox "testnum now is = " & testnum End Sub --- Public Sub test() testnum = 1 MsgBox "Before = " & testnum makebig (testnum) MsgBox "After = " & testnum End Sub test() does not change testnum, Before = 1, After = 1 what is happening? Thanks heaps

      K Offline
      K Offline
      Konstantin Vasserman
      wrote on last edited by
      #2

      Change line that reads: makebig (testnum) to makebig testnum If you use parentheses here it assumes that parameter you pass is not a variable but a result of an expression. Therefore instead of passing address of your testnum variable it passes an address of the result of the expression.

      1 Reply Last reply
      0
      • L Lost User

        The help file says VB uses pass-by-reference as a default(byval to use pass by value) - but the simple test I do in an Excel module isn't working Public testnum ----- Public Sub makebig(inpt) MsgBox "Make big receives = " & inpt inpt = inpt + 1 MsgBox "Input now is = " & inpt MsgBox "testnum now is = " & testnum End Sub --- Public Sub test() testnum = 1 MsgBox "Before = " & testnum makebig (testnum) MsgBox "After = " & testnum End Sub test() does not change testnum, Before = 1, After = 1 what is happening? Thanks heaps

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Try this

        Option Explicit

        Public testnum As Integer

        Public Sub MakeBig(inpt As Integer)
        MsgBox "Make big receives 'inpt' = " & inpt, vbOKOnly, "In MakeBig()"
        inpt = inpt + 1
        MsgBox "'inpt' now is = " & inpt, vbOKOnly, "In MakeBig()"
        MsgBox "testnum now is = " & testnum, vbOKOnly, "In MakeBig()"
        End Sub

        Public Sub Test()
        testnum = 1
        MsgBox "The original value of testnum = " & testnum, vbOKOnly, "In Test()"
        MakeBig testnum
        MsgBox "After MakeBig()= " & testnum, vbOKOnly, "In Test"
        End Sub

        Paul Lyons Do not go where the path may lead, go instead where there is no path and leave a trail. - Ralph Waldo Emerson

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups