This is a discussion on "byRef/byVal" within the Databases section. This forum, and the thread "byRef/byVal are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
byRef/byVal
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
#1
|
|||
|
|||
|
byRef/byVal
So I've been having some funcky stuff happening to data when I pass it into fuctions, especially information in arrays....
After *hours* of debugging, I'm sure that it's a byRev/byVal issue. I send a piece of data from an array into a fuction. Do some stuff in the function and it comes out changed! TaDah! :mad: So my questions is simply How do I Make It Stop! achem. Thanks jakyra |
|
|
|
#2
|
|||
|
|||
|
Is it something to do with a clash between duplicate variables having local and global scopes?
A variable defined outside a function will have global scope. If you change that variable's value inside a function, it will change it for the whole application. However, if you define a variable within a function it will only have local scope and will be destroyed when the functon has completed. If you define a local variable with the same name as a global variable you're going to run into the problems you describe, as the function will use make changes to the global variable by default. |
|
#3
|
|||
|
|||
|
The pitfalls of moving from VBA to VBScript. These are global variables, but in VBA if you pass the variable in a function and change the name a local copy is made. You have to have the names be the same to pass things byRef. I hadn't thought that it would be different in VBScript. So all I have to do is copy the parameter into a local variable and *voila* Happy variables. Thanks! I thought I was loosing my mind. jakyra |
|
#4
|
|||
|
|||
|
If you pass it in as a parameter, then the variable(s) defined in the function parameters will be automatically created like:
|
|
#5
|
|||
|
|||
|
well, my code looks like this:
books(i, 5) = replace(getArrayData("isbn" & i), "-", "") 'isbnNoHyph books(i, 0) = replace(getArrayData("isbn" & i), " " , "") 'isbn books(i, 3) = isIsbn(books(i,5)) 'valid (where getArrayData just finds a variable passed from the previous for that was loaded into an array) initially I had books(i, 0) = replace(getArrayData("isbn" & i), " " , "") 'isbn books(i, 5) = replace(books(i,0), "-", "") 'isbnNoHyph it also changes books(i,0) to not have any hyphens. and also if I have this line books(i, 3) = isIsbn(books(i,5)) 'valid and I use books(i,0) instead of (i,5) it also changes books(i,0) to not have any hyphens (isISBN removes the hyphens inside the function) but, it's still passed as a parameter. I've never encountered this before. My guess is that the arrays are getting passed byRef, that's the only thing I can think of. for the record isISBN looks like this:
jakyra |
|
#6
|
|||
|
|||
|
I see...
Do you have sample data returned by getArrayData("isbn" & i)? |
|
#7
|
|||
|
|||
|
I'm tempted to say that the way you're passing the value of the array location is causing the 'byRef' problem:
(in a non-syntactical sense) replace(books(i,0), "-", "") is saying: remove all the hyphens from the value of books(i,0) and update the array (essentially - ByRef) whereas something like (I've not tested it): replace(books(i,0).value, "-", "") is saying remove all hyphens from the actual value of books(i,0) and simply return the resulting string If the above doesn't work (or there's nothing similar I've not heard of), I'd be tempted to use a temp variable that the Replace() functions can parse before it's value is finally pushed into the Array, like: strResult = Replace(getArrayData("isbn" & i),"-","") books(i, 0) = strResult |
![]() |
| Tags |
| byrefbyval |
| Thread Tools | |
|
|