Passing variable ByRef not working?

jal

Registered User.
Local time
Today, 14:51
Joined
Mar 30, 2007
Messages
1,709
I pass the Keycode variable into a sub that changes its value from 107 to zero. Yet it returns as 107 - it should be returning as zero.


Private Sub tabBackWardsOnPlusSign(ByRef KeyCode As Integer)
If KeyCode = 107 Then 'plus sign
KeyCode = 0 'supress the plus sign
SendKeys "+{TAB}" 'tab backwards
End If
End Sub


Why isn't ByRef working?


This code is pretty simple. I just want the user to be able to tab backwards from textbox to textbox when he types the plus sign (which is Keycode 107).

Private Sub txtShortNo_KeyDown(KeyCode As Integer, Shift As Integer)
tabBackWardsOnPlusSign (KeyCode)
End Sub
 
And, yes, I have stepped through the code to make sure it is executing. The value does go from 107 to 0 in the called sub. And the sendKeys executes perfectly. The only thing that goes awry is that the caller sees the returned value as 107 instead of 0.

Not that there is no solution - but I am disturbed that I can't trust ByRef to do what it is supposed to do.
 
This: -

tabBackWardsOnPlusSign (KeyCode)

is forcing a pass by value that will override a receive by reference.

Remove the parenthesis and try again.


Regards,
Chris.
 
This: -

tabBackWardsOnPlusSign (KeyCode)

is forcing a pass by value that will override a receive by reference.

Remove the parenthesis and try again.


Regards,
Chris.

Thanks, you solved it.

I had no clue that parantheses could disable ByRef. Wow.
 
Chris

you pointed that behaviour out to me some time ago, and i didn't quite understand the relevance of what you were saying - having had a quick play, i do now

it does seem perverse that it should work like that - do you know where this is documented?

the plot thickens

as an aside, i checked my code, and I have used arguments in parenthesis as byref successfully - however, i am in the habit of putting "call" before subs/functions, (mainly for clarity) - and you also pointed out that this too changed the behaviour

so although

myfunc(x) passes x by value
call myfunc(x) passes x by ref

and you also pointed out the spacing changed

so what does "call" do, again please? - clearly it doesnt generate the same machine code at all

--------------------
note for jal

my understanding was that you shouldnt need even to declare byref, as arguments are byref by default
 
note for jal

my understanding was that you shouldnt need even to declare byref, as arguments are byref by default
Thanks for the reminder. In VB.Net the default is ByVal. To avoid confusion, I guess I'll just keep making this explicit in my code.
 
G’day Gemma.

Yes that behaviour is documented in the A97 help file somewhere but I can’t find it at the moment.

The Call word: -

Its use is optional but things change a little if it is or is not used.

Example: -

Code:
Function MyFunction(ByVal vntMyArgument As Variant) As Variant

    MyFunction = Null

End Function



Sub Test1()
    Dim X As Integer

    Call MyFunction(X)  [color=green]' OK[/color]
    
    MyFunction X        [color=green]' OK[/color]
    
    MyFunction (X)      [color=green]' OK[/color]

End Sub


Sub Test2()
    Dim X As Object
    
    Call MyFunction(X)  [color=green]' OK[/color]
    
    MyFunction X        [color=green]' OK[/color]
    
    MyFunction (X)      [color=green]' ERROR[/color]

End Sub

To me it’s a personal preference but I think that the use of the Call keyword has the potential to mask a possible error.
(Some people like to include it for code clarity but I’m still not convinced.)
And not that the error is all that important because it normally happens immediately when testing.

Another thing to notice is the all important space the editor inserts when attempting to force the pass ByVal.
That’s why I think people should cut and paste their code to site and not try to type it directly.
I don’t know anybody who can consistently write syntactically correct code.

So, from the point of view of parenthesis, there are two different types.

The first type, the one we are all familiar with, are those that enclose an argument list when calling a Function…

Fred = MyFunction(X, Y, Z)

The second type is the attempt to force the pass ByVal as in …

MyFunction (X)

Again, note the space.

Now we can mix and match arguments ByRef and ByVal…

Fred = MyFunction((X), Y, (Z))

This will attempt to force the pass ByVal of both X and Z but not Y.

Two many permutations to list but I would suggest, if I may, that people try to remove as much as possible from their code.
It seems to me that it’s the extraneous garbage that causes the problems sometimes.

Might I also suggest that people simply take the time to play with it when there is no pressure to perform.
Anyhow, that’s enough from me.

Regards,
Chris.
 
Thanks, Chris - this is apparently a much-neglected topic. God knows how much time I would have lost trying to figure this out on my own.
 
Yes Jal, it is a little known topic.

I learned the start of this about 5 years ago, from another site, and it has grown on me ever since.

But I think it comes down to this, and I keep trying to say it to myself every day;
KISS…Keep It Simple Stupid.

When we write a line of code, ask the question; why is that… there?

If there is no valid reason for it being there then…get rid of it.

Redundant code, references, external dependencies, even redundant characters can become a source of error in more ways than one.
(In this case, the extraneous parenthesis caused {actually masked} the problem.)

In other words; if in doubt…leave it out.

Regards,
Chris.
 

Users who are viewing this thread

Back
Top Bottom