Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBScript How To Return ArrayList From Function

Tags:

vbscript

I am trying to read a file into an array list and then return it from a function. My function works to read the file to an array but when I try setting it to a variable it errors saying invalid procedure call or argument

My read file code

function readfile(strFile)
dim fs,objTextFile 
set fs=CreateObject("Scripting.FileSystemObject")
dim arrStr
set objTextFile = fs.OpenTextFile(strFile)
Set userArrayList = CreateObject( "System.Collections.ArrayList" )

Do Until objTextFile.AtEndOfStream 
strNextLine = objTextFile.Readline 
userArrayList.add strNextLine
Loop 

objTextFile.Close
set objTextFile = Nothing
set fs = Nothing

readfile = userArrayList
end function

Calling it in my code

arr = readfile("\\dc1\NETLOGON\Scripts\Add_Users\user.csv")

For Each present In arr
user = split(present,",")
WScript.Echo user(0) & user(1) & user(2) & user(3) & user(4) & "|"
Next

What am I doing wrong?

like image 366
Talon06 Avatar asked Oct 24 '12 16:10

Talon06


1 Answers

Turns out I need to use set here is the working code. Working function

function readfile(strFile)
dim fs,objTextFile 
set fs=CreateObject("Scripting.FileSystemObject")
dim arrStr
set objTextFile = fs.OpenTextFile(strFile)
Set userArrayList = CreateObject( "System.Collections.ArrayList" )

Do Until objTextFile.AtEndOfStream 
strNextLine = objTextFile.Readline 
userArrayList.add strNextLine
Loop 

objTextFile.Close
set objTextFile = Nothing
set fs = Nothing

set readfile = userArrayList
end function

Working Call

Dim arr
set arr = readfile("\\dc1\NETLOGON\Scripts\Add_Users\user.csv")
For Each present In arr
user = split(present,",")
WScript.Echo user(0) & user(1) & user(2) & user(3) & user(4) & "|"
'WScript.Echo present & "|"
Next
like image 79
Talon06 Avatar answered Sep 24 '22 20:09

Talon06