Skip to main content

Script - ArrayList by function

I'm trying to create a function that takes an argument and returns to another function a list on which an operation is performed.

Sub Main
Call inputNew(inputValues("10", "26", "32"))
End Sub

Sub inputValues(ByVal Name1 As String, ByVal scores() String)
Set ArrayMyList = CreateObject("System.Collections.ArrayList")
' Use UBound to determine largest subscript of the array.
For i As Integer = 0 To UBound(scores)
fieldValue = scores(i)
ArrayMyList.Add fieldValue
Next i
'return function
inputValues ArrayMyList
End Sub

Function inputNew(ByRef ArrayList As Object, Value As String) Object
'Change values
ReDim MyList (ArrayList.Count)
For i = 0 To (ArrayList.Count - 1)
MyList(i) = ArrayList(i) + Value
Next i

'Return New Array
inputNew MyList
End Function

klmi Tue, 01/10/2023 - 10:45

Hello ernesto,
I'm not sure what exactly you're trying to do and wonder if the code you posted really makes sense (for example, why are you switching between array and ArrayList). In any case, the code contains several more or less major errors. I would definitely recommend you to have a look when to use "ByVal" and "ByRef". "ByRef" changes the source variable, but "ByVal" does not. In connection with arrays only the "ByRef" reference is possible, unless you use the type "Variant". I have tried to make your code running so far and hope the samples will help you.


Option Explicit

'creates ArrayList object from Array
Function createArrayList(ByVal scores As Variant) As Object
Dim i As Integer
Dim s As String
Dim thisArrayList As Object
Set thisArrayList = CreateObject("System.Collections.ArrayList")
For i = 0 To UBound(scores)
s = scores(i)
thisArrayList.Add s
Next i
'return function
Set createArrayList = thisArrayList
End Function

'modifies values from the ArrayList object by Value; source ArrayList object will be changed!
Sub modifyArrayList(ByRef ArrayList As Object, Value As String)
Dim i As Integer
'Change values
For i = 0 To (ArrayList.Count - 1)
ArrayList(i) = ArrayList(i) + Value
Next i
End Sub

'transforms ArrayList object to an Array
Function arrayListToArray(ByVal ArrayList As Object) As Variant
Dim i As Integer
ReDim sArr(ArrayList.Count - 1)
For i = 0 To (ArrayList.Count - 1)
sArr(i) = ArrayList(i)
Next i
arrayListToArray = sArr
End Function

'modifies values from the ArrayList object by Value; source ArrayList won't be changed
Function modifyArrayListToArray(ByVal ArrayList As Object, Value As String) As Variant
Dim i As Integer
'Change values
ReDim sArr(ArrayList.Count - 1)
For i = 0 To (ArrayList.Count - 1)
sArr(i) = ArrayList(i) + Value
Next i
modifyArrayListToArray = sArr
End Function

'Main routine
Sub Main()
Dim oArrayList As Object

Dim sArr() As String
sArr = Split("10,20,30", ",")

Set oArrayList = createArrayList(sArr)

'modifies values from ArrayList object and changes source ArrayList object
Call modifyArrayList(oArrayList, "100")
MsgBox (oArrayList(1))

'transform ArrayList object to Array
Dim s1() As Variant
s1 = arrayListToArray(oArrayList)
MsgBox (s1(1))

'modifies values from ArrayList object but doesn't change source ArrayList object
Dim s2() As Variant
s2 = modifyArrayListToArray(oArrayList, "200")
MsgBox (s2(1))

'not changed ArrayList object
MsgBox (oArrayList(1))
End Sub