Results 1 to 11 of 11

Thread: Very general Question: Writeback a Parameter

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 1999
    Location
    Germany
    Posts
    17

    Post

    Hi everybody,

    a long time I'm fed up with writing:

    Code:
    intThisIncreasingVariable = intThisIncreasingVariable + 1
    And so I decided to write the same Sub known from Turbo Pascal: "Inc"

    The hole stuff should look like this. I think you then will know what I mean:

    Code:
    Private Sub Form_load()
      Dim TestVar as integer
      TestVar = 1
      inc(TestVar)
      MsgBox "Should be 2 but is only: " & TextVar
    End Sub
    
    Public Sub inc(ByRef x, Optional step = 1)
      x = x + step
    End Sub
    But in Basic it seems to be impossible to write back to the Variable in the calling statement. Is this right?

    Regards,
    Frank


  2. #2
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    355

    Post

    yeah, u cant write back to the var passed directly, i think (correct if wrong)
    just put it in a function and call it like:

    VarUWantIncreasing = Inc(VarUWantIncreasing)

    Function:

    [CODE]Public Function Inc(X as long, Optional Step = 1)

    x = x + step
    Inc = X

    Enc Function[\CODE]

    in fact i'djust carry on writing x = x + 1



    ------------------
    cintel rules
    www.cintelsoftware.co.uk

  3. #3
    Addicted Member
    Join Date
    Oct 1999
    Location
    Oporto, Portugal
    Posts
    134

    Post

    Just a small corection:

    Public Function Inc(X as long, Optional Step = 1) as integer


    So, as far as I know, you have to declare the type of the variable you want to return, even if it is a variant.

    If oyu want to return deiferent type return it as a variant and then convert it to what you want (long, double, date, etc).


    ------------------
    Jorge Ledo
    [email protected]
    Portugal

  4. #4
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    Rotterdam, Netherlands
    Posts
    386

    Post

    Change
    inc(TestVar)
    to
    Call inc(TestVar)
    and it works.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 1999
    Location
    Germany
    Posts
    17

    Post

    Wow!
    Not bad Crazy....

    Thanks

    Can you explain it to me? Why does it run this way?

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Dec 1999
    Location
    Germany
    Posts
    17

    Post

    All right!
    I got it:
    Here this is also very got or even better :-)

    inc myVar

    or

    inc myVar, 5

    I remember:
    If you use brackets the parameter is passed by value and if not then it is passed by reference.

    All right. Thanks a lot!!

    Regards,

    ------------------
    -------------
    >> Frank <<


    [This message has been edited by Frankiz (edited 01-04-2000).]

    [This message has been edited by Frankiz (edited 01-04-2000).]

  7. #7
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    355

    Post

    can u give me a definition of by value and by reference?
    cos i see all these ByRef and ByVal 's and its confusing

    ------------------
    cintel rules
    www.cintelsoftware.co.uk

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Dec 1999
    Location
    Germany
    Posts
    17

    Post

    Here is the definition from the MSDN (msdn.microsoft.com) or from CD:

    by reference
    ------------
    A way of passing the address of an argument to a procedure instead of passing the value. This allows the procedure to access the actual variable. As a result, the variable's actual value can be changed by the procedure to which it is passed. Unless otherwise specified, arguments are passed by reference.


    by value
    --------
    A way of passing the value of an argument to a procedure instead of passing the address. This allows the procedure to access a copy of the variable. As a result, the variable's actual value can't be changed by the procedure to which it is passed.


    I hope this could help. I don't know what I should add.
    In our example it is necessary that the Inc-Sub works with the value stored on the adress and not with a copy of the parameter.

    Regards,
    Frank

  9. #9
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    355

    Post

    cheers m8 i get it now.. its a bit like pointers in C++


    ------------------
    cintel rules
    www.cintelsoftware.co.uk

  10. #10
    Guest

    Post

    Exactly! In fact when passing by value you are creating another instance of the variable in memory and are thus consuming more resources. If you pass by reference then you are passing a pointer (address) to the function, and therefore you are addressing the location of the only variable in memory, and changing it globally, without recreating it in the local scope of the called function.

  11. #11
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    355

    Post

    quite - so most of the time byref is better than byval.
    but why do most API calls use ByVal?

    ------------------
    cintel rules
    www.cintelsoftware.co.uk

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