|
-
Jan 3rd, 2000, 07:16 PM
#1
Thread Starter
Junior Member
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
-
Jan 3rd, 2000, 07:26 PM
#2
Hyperactive Member
-
Jan 3rd, 2000, 07:38 PM
#3
Addicted Member
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
-
Jan 3rd, 2000, 08:27 PM
#4
Hyperactive Member
Change
inc(TestVar)
to
Call inc(TestVar)
and it works.
-
Jan 3rd, 2000, 08:31 PM
#5
Thread Starter
Junior Member
Wow!
Not bad Crazy....
Thanks
Can you explain it to me? Why does it run this way?
-
Jan 3rd, 2000, 08:36 PM
#6
Thread Starter
Junior Member
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).]
-
Jan 4th, 2000, 02:52 AM
#7
Hyperactive Member
-
Jan 4th, 2000, 05:05 PM
#8
Thread Starter
Junior Member
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
-
Jan 4th, 2000, 07:10 PM
#9
Hyperactive Member
cheers m8 i get it now.. its a bit like pointers in C++
------------------
cintel rules 
www.cintelsoftware.co.uk
-
Jan 6th, 2000, 11:06 PM
#10
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.
-
Jan 7th, 2000, 03:41 AM
#11
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|