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

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)

Related

var Array of objects not initialising [duplicate]

This question already has an answer here:
Function returns an empty List in Spark
(1 answer)
Closed 4 years ago.
I've a following code :-
case class event(imei: String, date: String, gpsdt: String, entrygpsdt: String,lastgpsdt: String)
object recalculate extends Serializable {
def main(args: Array[String]) {
val sc = SparkContext.getOrCreate(conf)
val rdd = sc.cassandraTable("db", "table").select("imei", "date", "gpsdt").where("imei=? and date=? and gpsdt>? and gpsdt<?", entry(0), entry(1), entry(2), entry(3))
var lastgpsdt = "2018-04-06 10:10:10"
var updatedValues = new Array[event](rdd.count().toInt)
var index = 0
rdd.foreach(f => {
val imei = f.get[String]("imei")
val date = f.get[String]("date")
val gpsdt = f.get[String]("gpsdt")
updatedValues(index) = new event(imei, date, gpsdt,lastgpsdt)
println(updatedValues(index).toString())
index = index + 1
lastgpsdt = gpsdt
})
println("updates values are " + updatedValues.toString())
}}
So, here I'm trying to create an array of event class answer save values in array on each iteration and want to access the array outside foreach block. My issue is when I'm trying to access the array it gives null pointer exception and i checked it shows the array is empty. Although I have declared the array as var still why not able to access outside. Suggestions please, Thanks.
If you want to get Array[event] then I don't think that is the right approach
Here is what you can do for alternative
case class event(imei: String, date: String, gpsdt: String,
entrygpsdt: String,lastgpsdt: String)
val result = rdd.map(row => {
val imei = row.getString(0)
val date = row.getString(1)
val gpsdt = row.getString(2)
//create case class as you want
event(imei, date, gpsdt, lastgpsdt ,"2018-04-06 10:10:10")
})
.collect()
The result you obtain is Array[event]
Collect is also preferred only when your data size is small and can fit in a driver.
Hope this helps!

Return array of objects from function - VBA

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

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

data to structure in vb.net

I have this structure in Visual Basic .net
Private Structure queueT
Public name As String
Public parent As String
Public limitat As Integer
Public maxlimit As Integer
And this is the data I get, which I have stored in a one dimensional String array (just pasting 3):
!re=.id=*10000B0=name=Up-PBX=parent=Up=packet-mark=pack_pbx=limit-at=256000=queue=PCQ_Up=priority=1=max-limit=512000=burst-limit=0=burst-threshold=0=burst-time=00:00:00=invalid=true=disabled=true=comment=PBX
!re=.id=*10000C7=name=Down_Mauro=parent=Down=packet-mark==limit-at=315000=priority=8=max-limit=5000000=burst-limit=0=burst-threshold=0=burst-time=00:00:00=invalid=false=disabled=true
!re=.id=*10000C8=name=Down_Mauro_dom=parent=Down_Mauro=packet-mark=pack_Mauro_dom=limit-at=40000=queue=PCQ_Down=priority=2=max-limit=400000=burst-limit=0=burst-threshold=0=burst-time=00:00:00=invalid=false=disabled=true
I need to store the information in my structure so it looks like this:
queueT.name = UP-PBX
queueT.parent = UP
queueT.limitat = 256000
queueT.maxlimit = 512000
I only need the information mentioned above, not the rest. How can I do that?
Thanks!!
It looks like it's a key/value pairing after the first value. From your sample something like this could work.
Private Function queueTParse(item As String) As queueT
Dim queueValues = item.Split("=")
Dim queueTItem = New queueT
For i As Integer = 1 To queueValues.Length - 1 Step 2
Select Case queueValues(i)
Case "name"
queueTItem.name = queueValues(i + 1)
Case "parent"
queueTItem.parent = queueValues(i + 1)
Case "limit-at"
queueTItem.limitat = queueValues(i + 1)
Case "max-limit"
queueTItem.maxlimit = queueValues(i + 1)
End Select
Next
Return queueTItem
End Function

as3 passing a string from an array to a function

I have an empty array collecting a hex value thats randomly selected from another array within a function. i am trying to pull the hex value from the array that gets filled and pass it to another function to randomly change the color value of a particle system...
private var ca:Array = new Array();
private var rc:String = ca; // pseudo...this is the string that needs to get passed
public function addCursor(cursor:Cursor):void {
var cc:Array = new Array("0xFFFF33", "0xFFFFFF", "0x79DCF4", "0xFF3333", "0xFFCC33", "0x99CC33");
var rcc:String = cc[Math.floor(Math.random() * (cc.length))];
ca.push(rcc); //
trace(rcc + ' 1st array');
trace(ca + ' 2nd array');
trace(rc + ' string to pass');
// unrelated stuff happens down here...
the 1st and 2nd arrays both trace the same hex value, but i cant find the right way to capture that string...ive tried several different methods that all return 'null' ...which makes me think maybe the value is leaving the array before i try to snag it?
i removed ca.pop() which is called in a later function, just to see if rc would still return a null and it does.
rcc is a string, not an array.
So
trace(rcc + ' chosen hex as a string');
But I don't see why you need an array (ca) just to hold a single string.
This would work:
private var rc:String;
private var cc:Array = new Array("0xFFFF33", "0xFFFFFF", "0x79DCF4", "0xFF3333", "0xFFCC33", "0x99CC33");
public function addCursor(cursor:Cursor):void {
rc = cc[Math.floor(Math.random() * (cc.length))];
trace(rc + ' the chosen hex');
}

Resources