Return array of objects from function - VBA - arrays

I need to "return" an Array of Objects from a function that I created using VBA. When I try to set the function as the array it gives me an error message, saying
Object is required.
I am not very used to VBA, and I can't fix this. Here is the function code:
Function sortedList(listRange As Integer, tempList() As ship) As ship
Dim temp As ship
Set temp = Nothing
For i = listRange - 10 To 1 Step -1
For j = 2 To listRange - 10
If tempList(j - 1).Arrival > tempList(j).Arrival Then
Set temp = tempList(j - 1)
Set tempList(j - 1) = tempList(j)
Set tempList(j) = temp
End If
Next j
Next i
'return tempList - how?
Set sortedList = tempList
End Function
Ship is a "class" that I created. tempList is the array of objects from class ship that I need to return from the function sortedList.
The function works, it's just the return part that I can't make work.
Thanks for the help. If more information is necessary let me know!

Declare the function to return an array
Function sortedList(listRange As Integer, tempList() As ship) As ship()
and then assign the result without Set
sortedList = tempList

Related

SSRS Median and drill down reports

I have a SQL Server Reporting Services report in which I calculate the median using an ArrayList in a custom code block as median is not a built in function in SSRS. The median and average are displayed in a column chart. The report includes a drilldown to a detail report. If I drill down to the detail report and then hit the back arrow in the browser to navigate back to the master report, the median on the master report is not being calculated. That column of the column chart is empty while the average is displayed as it should.
Does anyone have any thoughts on why this is happening or how I can fix it?
The code is below. AddValue() first creates a new SortedList of it doesn't already exist. It then adds the newValue to an ArrayList keyed by the string passed to the function, creating one if it didn't previously exist. Each ArrayList represents a different set of values for which we would want to calculate the median.
GetMedian() calculates the median for the ArrayList identified by the passed string. GetMedian() is used in the value for the column in the chart.
Public Dim ClearanceList As System.Collections.SortedList
Function AddValue(ByVal whichList As String, ByVal newValue As Integer) As Decimal
Dim thisList As System.Collections.ArrayList
If (ClearanceList is Nothing) Then
ClearanceList = New System.Collections.SortedList
End If
If (ClearanceList.ContainsKey(whichList)) Then
'Get a reference to the desired ArrayList and add the new value to it.
thisList = ClearanceList.GetByIndex(ClearanceList.IndexOfKey(whichList))
Else
thisList = New System.Collections.ArrayList()
' Create a new element in the SortedList
ClearanceList.Add(whichList, thisList)
End If
thisList.Add(newValue)
AddValue = thisList.Count
End Function
Function GetMedian(ByVal whichList As String) As Decimal
Dim thisList As System.Collections.ArrayList
Dim count As Integer
thisList = ClearanceList.GetByIndex(ClearanceList.IndexOfKey(whichList))
count = thisList.count
If (count > 0) Then
thisList.Sort()
If count Mod 2 = 1 Then
GetMedian = thisList((count - 1) / 2)
Else
GetMedian = (thisList((count / 2) - 1) + thisList((count / 2))) / 2
End If
Else
GetMedian = -1
End If
End Function

How to retrieve a VB Control instance, given its Name and Index?

If I have a string "cboEnoughMoney(0)", is it possible to retrieve the control from a control array that I have in my form, cboEnoughMoney?
In my code, I try to refer to the control in this way:
frm.Controls(sControlName).ListIndex = -1
sControlName in this case is "cboEnoughMoney(0)" and I get an error that the control was not found.
EDIT: One of the issue's i'm having is trying to as above set the .listindex to -1, another one is where I actually try to get and set value to the combobox. In a function - i pass form name as parameter called frm.
In the code what has been done is this....
dim ctlData as string
If Frm.Controls(RS!CTRLname).ListIndex > 0 Then
CtlData = Frm.Controls(RS!CTRLname).Value
Else
CtlData = ""
End If
Any idea how I'd be able to do that with cboEnoughMoney(0)....
I assume I could follow your example and check
if instr(1,RS!CTRLname, "(")
but if there is, how would I refer to that particular control in frm
It is just enough to look in the VB Properties Window: if you search "cboEnoughMoney(0)" you will find "cboEnoughMoney"(0) (without double quotes). Still the name is the same as a non-array control.
So, it is perfectly legal to refer to a VB Control with Name and Index, no need for a controls loop here:
If isControlsArray Then
Set ctrl = Me.Controls(Name)(Index) '// array
Else
Set ctrl = Me.Controls(Name) '// non array
End If
You cannot use the (index) part in a lookup via .Controls.
You can loop them manually & given that you can safely assume any control name with a ( is an array member and thus will have an Index property match on that:
Private Function GetControl(ByVal name As String) As VB.Control
Dim pos As Long: pos = InStr(name, "(")
If pos = 0 Then
Set GetControl = Me.Controls(name) '// non array
Else
Dim index As Long
index = Val(Mid$(name, pos + 1)) '// get index #
name = Left$(name, pos - 1) '// get base name
For Each GetControl In Me.Controls
If (GetControl.name = name) Then
If (GetControl.index = index) Then Exit Function
End If
Next
Set GetControl = Nothing
End If
End Function
And then:
GetControl("cboEnoughMoney(1)").ListIndex = -1

Error when setting an array index with an object in vbscript?

I'm trying to create an array of objects in vbscript, where each object has a string and a number as properties. The string is coming from a different array, and the number is incremented in the loop.
Here's the error occurring, on the line newValues(i) = (New Pet)(values(i), number):
...and here's my code:
Class Pet
Public objectName
Public objectNumber
' constructor here:
Public Default Function Init(name, number)
objectName = name
objectNumber = number
Set Init = Me
End Function
End Class
values = Array(_
"Cat",_
"Dog",_
"Bird"_
)
number = 3
ReDim newValues(uBound(values))
For i = 0 to uBound(values)
newValues(i) = (New Pet)(values(i), number)
number = number + 1
Next
Use Set when assigning objects.
Set newValues(i) = (New Pet)(values(i), number)

dynamic array of objects in Matlab

I have created this two codes classes.
classdef master < matlab.mixin.Copyable
properties
id
list
end
methods
function this=master(id)
if nargin > 0
this.id = id;
this.list = repmat(msg,1,20);
end
end
end
end
classdef msg < matlab.mixin.Copyable
properties
id
dest
ttl
end
methods
function this=msg(id,dest,ttl)
if nargin > 0
this.id = id;
this.dest = dest;
this.ttl = ttl;
end
end
end
end
In another part of my code I try to delete one or more objetc "msg" from array "master.list" using the following:
function verifyMsgToDiscard(this,t)
i = 1;
while (i <= numel(this.list))
m = this.list(i);
if (t > m.ttl)
this.list = this.list(this.list~=m); %remove m of the list
clear m; %delete m from the system
end
i= i + 1;
end
end
I am getting the error:
Index exceeds matrix dimensions.
Error in master/verifyMsgToDiscard (line 117)
m = this.list(i);
The problem I think is why I am iterating over the master.list in the same time I modify the number of elements on it. In addition I can add and remove new objects "msg" in the "list" then its size varies. How I can do this in a dynamic way.
I suppose you're trying to remove msg objects having ttl to be smaller than t. This is the Matlab way of removing elements:
this.list = this.list(t <= [this.list.ttl]);
Note that t <= [this.list.ttl] generates a logical index.

Re-assigning an array in ASP classic

I have the following function, designed to walk through XML and create a linear structure of all nodes:
function get_children(n)
if n.hasChildNodes() then
for each child in n.childNodes
set local_array = array_merge(get_children(child), local_array)
next
else
set local_array = Array(n)
end if
get_children = local_array
end function
I've tried a ton of variations, but I keep getting errors on the line
set local_array = Array(n)
It it's current form, I see:
Error Type:
Microsoft VBScript runtime (0x800A01A8)
Object required
/_inc/nav/left-nav.inc, line 37
Am I mis-using the Array() construct? Aren't I able to create an array with a single value?
Change
set local_array = Array(n)
to
local_array = Array(0)
set local_array(0) = n

Resources