Functions in VBscript default as byref

This forum is meant for requesting technical support or reporting bugs.

Moderators: time-killer-games, Vengeance66, Candle, reneuend, GM-Support

Functions in VBscript default as byref

Postby reneuend » Wed Oct 14, 2009 3:03 am

So, I was building a simple for loop that contains a call to a function that I created. The function takes as one of it's parameters, an index. The odd thing is that I ALWAYS use a unique name in the function parameter from the value that is being passed in.

example: x = GetWord(counter, text, delimiter)

formal definition: Function GetWord(idx, sentence, delim)

So, as you can see, idx is a copy of counter.

There 2 ways the process can use idx:
ByVal: If you change idx in the function, it does NOT change the value outside the function

ByRef: If you change idx in the function, it WILL change what was passed in. In this example, counter would be the same value as idx when the function was finished.

After all this time, I just learned the hard way that VBScript defaults to using "ByRef"!!!!! So, when I changed the IDX value it changed my counter of my loop. Since I was subtracting 1 from IDX, I was in an endless loop! :shock:

To fix it, I just put "ByVal" in front of IDX like this:

Function GetWord( ByVal idx, ByVal sentence, ByVal delim)

I known for years about using byval and byref, but I never realized that byref was the default. It's important to know the difference....as I found out the hard way.
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2762
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA

Postby ShadowHunter » Wed Oct 14, 2009 9:22 am

Hehehe, been there :D
User avatar
ShadowHunter
Forum Admin and Games Page admin
 
Posts: 1304
Joined: Fri Jun 06, 2003 10:37 pm
Location: Belgium

Postby Lyberodoggy » Wed Oct 14, 2009 5:25 pm

Yeah, me too :D
User avatar
Lyberodoggy
Administrator
 
Posts: 2526
Joined: Sat Feb 17, 2007 3:31 pm
Location: Athens


Return to Adventure Maker Technical Support and Bug Reports

Who is online

Users browsing this forum: No registered users and 0 guests

cron