PDA

Click to See Complete Forum and Search --> : value question


netSurfer
Jan 21st, 2000, 02:45 AM
A friend had given me some code to go through a csv file and grab out a count of the number of fields as well as the actual info. It works but I have a question and he's not available at the moment.

I have the code to do this in a seperate function.

function ScanLine(iLine as string, strFields() as string) as integer

hte iLine is the entire line from the csv file. The strFields array is used to store the value and ScanLine returns a count of the number of fields. The function that calls it has a strFields() array declared as well. It seems that it runs this 2 times, 1 to get a count of fields, then ReDim's the strFields array with that number. Then it runs it again and this time it puts the value into the strFields array. I don't understand how 1 function can affect a variable in another function. Neither are globals or anything. They're just plain string declarations. If someone can shed some light on this I would appreciate it.
Thanks.

Aaron Young
Jan 21st, 2000, 03:43 AM
When Declaring/Creating Functions/Subs Parameters are passed ByRef by default, which means the Sub/Function gets a Pointer to the Actual Variable, not just the Value of the Variable, so making a Change to the Parameter would Directly Change the Variable Passed to it, ie.

Private Sub Add2(iValue As Integer)
iValue = iValue + 2
End Sub

If you call Add2 and Pass it a Variable, that Variable will be Incremented by 2..

Private Sub Add2(ByVal iValue As Integer)
iValue = iValue + 2
End Sub

Calling this Version of Add2 with the Same Variable, will never change the Variables Value as the Sub is Recieving the Value of the Variable and Storing it in a Local/Private Variable whose Scope Only Exists within the Sub.

------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
ajyoung@pressenter.com

netSurfer
Jan 21st, 2000, 05:04 AM
Thanks, that makes sense.