byRef/byVal

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.



Go Back   Webforumz.com > Main Forums > Program Your Website > Databases

Notices


Closed Thread
 
LinkBack Thread Tools
  #1 (permalink)  
Old Oct 8th, 2003, 21:40
Reputable Member
Join Date: Sep 2003
Location: USA
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
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 (permalink)  
Old Oct 8th, 2003, 22:08
Reputable Member
Join Date: Aug 2003
Location: United Kingdom
Posts: 341
Thanks: 0
Thanked 0 Times in 0 Posts
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 (permalink)  
Old Oct 8th, 2003, 22:32
Reputable Member
Join Date: Sep 2003
Location: USA
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
AHHH!

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 (permalink)  
Old Oct 8th, 2003, 23:05
Reputable Member
Join Date: Aug 2003
Location: United Kingdom
Posts: 341
Thanks: 0
Thanked 0 Times in 0 Posts
If you pass it in as a parameter, then the variable(s) defined in the function parameters will be automatically created like:
Code: Select all
Dim glParam1, glResult

glParam1 = 7

glResult = fnDeduct(glParam1)

<font color="red">response.write lcParam1</font id="red">

function fnDeduct(lcParam1)
    fnDeduct = lcParam1 - 1
end function
In this case, when execution has completed:<ul>[*]glParam1 maintains a value of 7[*]lcParam1 is created temporarily (for the duration of execution of the function) and is automatically assigned the same value as the passed parameter[*]glResult takes the value returned from the function - 6[*]execution of the last line (in red) should generate an error because that variable dwas not global and as such, no longer exists[/list]
  #5 (permalink)  
Old Oct 9th, 2003, 13:17
Reputable Member
Join Date: Sep 2003
Location: USA
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
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:


Code: Select all
<%
	Function IsIsbn(thisISBN)
	'upgrade to include 978 prefix
			'tests if value is legitimate thisISBN number
			Dim  intI , intSum , thisInt, HasFormat, thisISBN

			IsIsbn = False
			If Not IsNull(thisISBN) Then
				'Clean: Remove hypehens and spaces
				thisISBN = Replace(thisISBN, "-", "")
				thisISBN = Replace(thisISBN, " ", "")

				'Rule 1:     number must be exactly 10 units, the first 9 numbers, the last either a number or X
				HasFormat=False
				If len(thisISBN)=10 then
					if IsNumeric(Left(thisISBN, 9)) then
						if IsNumeric(Right(thisISBN,1)) or lcase(Right(thisISBN,1)) = "x" then
							HasFormat = True
						end if
					end if
				end if

 				If HasFormat then
 				'Rule 2:     All numbers, weighted by position, must sum to a number that is divisible by 11
					'Sum the digits by weight
					intI = 0
					Do While intI < 9
						intI = intI + 1
							thisInt = Mid(thisISBN, intI, 1)
							if IsNumeric(thisInt) then 	intSum = intSum + ((11 - intI) * cInt(thisInt))
					Loop
					'Add the check sum number
					intI = intI + 1

					If Ucase(Right(thisISBN, 1)) = "X" Then intSum = intSum + 10 Else intSum = intSum + cInt(Right(thisISBN,1))
					'and check for base 11 match
					IsIsbn = (intSum Mod 11 = 0)
				End If
			End If
	End Function

%>
Thanks
jakyra
  #6 (permalink)  
Old Oct 9th, 2003, 14:45
Reputable Member
Join Date: Aug 2003
Location: United Kingdom
Posts: 341
Thanks: 0
Thanked 0 Times in 0 Posts
I see...
Do you have sample data returned by getArrayData("isbn" & i)?
  #7 (permalink)  
Old Oct 9th, 2003, 14:56
Reputable Member
Join Date: Aug 2003
Location: United Kingdom
Posts: 341
Thanks: 0
Thanked 0 Times in 0 Posts
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
Closed Thread

Tags
byrefbyval

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 19:51.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43