Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Byref,Byval question 1

Status
Not open for further replies.

MrMoocow

Programmer
May 19, 2001
380
US
Byref Byval and errors: (oh my?)

Okay I have a wrap function:

Very simple (gets the job done)

Public Function Wrap(Value As Integer, Max As Integer, Min As Integer) As Integer

If Value = Max + 1 Then
Wrap = Min
Else
Wrap = Value
End If


End Function

Now when I call this

Bob = wrap(bob,20,0)

it gives Byref error,

I know how to fix this, but can someone explain or point me to a resource that explains what byref and byval do?


Thank you very much,
Brad,
Hey! email me any time! Bradsvb@yahoo.com
 
Hi Brad - this is my basic understanding of ByRef, ByVal

When you declare a procedure, you have the option of specifying whether you are passing the arguments that it uses by reference or by value.

e.g.
Sub Procedure1(ByVal intArgument1 as Integer)

Sub Procedure1(ByRef intArgument1 as integer)

If you pass the argument by reference, any changes that you make to that argument are permanent.

If you pass the argument by value, changes made to the argument in the procedure you call do not affect the value of the original variable.

So, if you want to pass the value of a variable into a procedure, but don't want to change the original variable,
pass the argument by value.

If you want to retain any changes that the procedure makes to the variable or argument, pass by reference.

If you do not specify either, the default is for
arguments to be passed by reference (original variable is changed)



Another way to think of it is this.

If you pass by value, you make a copy of the variable
and use that copy instead of the original in the procedure. When the procedure is finished, you throw out the copy and any changes made to it and you still have the original variable with its' original value.
If you pass by reference, you just use the original variable
and any changes you make to it in the procedure are permanent.

Wow - this seems like an easy concept in my head, but hard
to put in words. Hope this helped :)
 
Are you getting a ByRef argument type mismatch error? if so, check very carefully how Bob is declared.

 
Strongm hit it, the only varuable passed is bob. The others are definately integers. Bob must be declared to match the parameter type. Check him out.


Other tips,
NEVER use equals tests unless you absolutely mean equals! Besides testing for = max + 1 vs. > max is extra work. Code what you mean, it'll be easier to debug later.
[tt]
Wrap = Value ' Assume Value is OK
If Value > max then Wrap = min ' OOps, was wrong
[/tt]or [tt]
Wrap = Iif(Value>Max, Min, Value) ' Less pushing and popping
[/tt]

If you want to use increase the versitility of your function and create an if-less process:
[tt]
Range = ((Max - Min) + 1)
Wrap = ((Value - Min) mod Range) + Min
[/tt]


Wil Mead
wmead@optonline.net

 
Thanks for all the help,
Like I said I had it working I just wanted to no what they meant,

And about useing equals,
My project is based on a grid, so things are always added in increments of one, but I guess I should change it anyways.


Brad,
Hey! email me any time! Bradsvb@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top