More weird array assignments

thedesolatesoul

Expert
Licensed User
Longtime User
I have this problem before, and had solved it with kickaha's help by simply Re-Dimming arrays/types before assigning them somewhere.

Here I am having trouble again and Re-dimming doesnt help:

Narrowed it down to this function:
TaskItems is a custom type array in Process Globals of a service.
B4X:
   Dim TaskItems(20) As TaskItemType

B4X:
Sub CreateTask(TaskType As Int, ScheduledTime As Long,Pipe As String, SourcePath As String, DestPath As String, Filter As String)
   Log("DB Created Task: Que" & dpservice.TotalQueItems & " Dest:" & SourcePath)
   'Re-Dim
   ' List all tasks
   Log("Before Create: List of all tasks: (Items:" & dpservice.TotalQueItems & ")")
   Log("==================")
   For i = 0 To dpservice.TotalQueItems -1
      Log(dpservice.TaskItems(i).QueIndex & " " & dpservice.TaskItems(i).SourcePath) 
   Next
   Log("===================")
      
   dpservice.TaskItems(dpservice.TotalQueItems).Initialize
   dpservice.TaskItems(dpservice.TotalQueItems).TaskType = TaskType
   dpservice.TaskItems(dpservice.TotalQueItems).Pipe = Pipe
   dpservice.TaskItems(dpservice.TotalQueItems).ScheduledTime  = ScheduledTime
   dpservice.TaskItems(dpservice.TotalQueItems).SourcePath = sourcepath
   dpservice.TaskItems(dpservice.TotalQueItems).DestPath  = DestPath
   dpservice.TaskItems(dpservice.TotalQueItems).TaskCommand = filter
   dpservice.TaskItems(dpservice.TotalQueItems).Processing = False
   dpservice.TaskItems(dpservice.TotalQueItems).Qued = True
   dpservice.TaskItems(dpservice.TotalQueItems).QueIndex = dpservice.TotalQueItems
   dpservice.TotalQueItems = dpservice.TotalQueItems + 1
   ' Start service at ScheduledTime
   '!!! Do not create if Pipe/task already exists
   
      ' List all tasks
   Log("After Create: List of all tasks: (Items:" & dpservice.TotalQueItems & ")")
   Log("==================")
   For i = 0 To dpservice.TotalQueItems -1
      Log(dpservice.TaskItems(i).QueIndex & " " & dpservice.TaskItems(i).SourcePath) 
   Next
   Log("===================")

   StartServiceAt("dpservice",ScheduledTime,False)
End Sub

where dpservice is a service.

Anyways, the relevant logs before and after running this code are:

B4X:
Before Create: List of all tasks: (Items:1)
==================
0 https://api.dropbox.com/0/metadata/dropbox/Public?
===================
After Create: List of all tasks: (Items:2)
==================
1 https://api.dropbox.com/0/metadata/dropbox/Photos/Sample%20Album?
1 https://api.dropbox.com/0/metadata/dropbox/Photos/Sample%20Album?
===================

Yet, I have tried re-initialising and redimming the array but nothing works.
Any ideas?? :confused::confused:

EDIT:
Actually the problem is in a loop in the service:
B4X:
For i = 0 To TotalQueItems - 2
            Dim TempTI As TaskItemType 
            TempTI.Initialize
            TempTI = TaskItems(i + 1)
            TaskItems(i) = TempTI
            TaskItems(i).QueIndex = TaskItems(i).QueIndex - 1
         Next
I thought the Variable TempTI should take care of de-linking the reference. But apparantly not.
Still havent fixed it though.
 
Last edited:

thedesolatesoul

Expert
Licensed User
Longtime User
I solved this by doing:

B4X:
Sub CopyTask(T1 As TaskItemType) As TaskItemType 
   Dim T2 As TaskItemType 
   T2.Initialize 
   T2.DestPath=T1.DestPath 
   T2.Pipe = T1.Pipe 
   T2.Processing = T1.Processing 
   T2.Qued = T1.Qued 
   T2.QueIndex = T1.QueIndex 
   T2.ScheduledTime = T1.ScheduledTime 
   T2.SourcePath = T1.SourcePath 
   T2.TaskCommand = T1.TaskCommand 
   T2.TaskType = T1.TaskType 
   Return T2
End Sub

Its not really intuitive though.
Can we not pass objects by value rather than reference?

[Also as a side question: how do you paste code in color in the forums?]
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
For i = 0 To TotalQueItems - 2
Dim TempTI As TaskItemType
TempTI.Initialize
TempTI = TaskItems(i + 1) ' You are losing the object you just redeclared!
TaskItems(i) = TempTI
TaskItems(i).QueIndex = TaskItems(i).QueIndex - 1
Next

Can we not pass objects by value rather than reference?
No. Strings and anything other than primitive numeric types are passed as a reference in Java. Passing complex objects by value would be very inefficient.

how do you paste code in color in the forums?
Select it and press the drop-down arrow by the big black A.
 
Upvote 0
Top