Click to See Complete Forum and Search --> : Very general Question: Writeback a Parameter
Frankiz
Jan 3rd, 2000, 06:16 PM
Hi everybody,
a long time I'm fed up with writing:
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:
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
KENNNY
Jan 3rd, 2000, 06:26 PM
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 :p
www.cintelsoftware.co.uk
JorgeLedo
Jan 3rd, 2000, 06:38 PM
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
j_ledo@hotmail.com
Portugal
Crazy D
Jan 3rd, 2000, 07:27 PM
Change
inc(TestVar)
to
Call inc(TestVar)
and it works.
Frankiz
Jan 3rd, 2000, 07:31 PM
Wow!
Not bad Crazy....
Thanks
Can you explain it to me? Why does it run this way?
Frankiz
Jan 3rd, 2000, 07:36 PM
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).]
KENNNY
Jan 4th, 2000, 01:52 AM
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 :p
www.cintelsoftware.co.uk
Frankiz
Jan 4th, 2000, 04:05 PM
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
KENNNY
Jan 4th, 2000, 06:10 PM
cheers m8 i get it now.. its a bit like pointers in C++
------------------
cintel rules :p
www.cintelsoftware.co.uk
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.
KENNNY
Jan 7th, 2000, 02:41 AM
quite - so most of the time byref is better than byval.
but why do most API calls use ByVal?
------------------
cintel rules :p
www.cintelsoftware.co.uk
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.