Hello,
I'm having some trouble with Xbasic and creating an array of objects to store data.
I have a for each loop, and within that loop I dim result as p. I store information in that result object, add it to a dynamic array, and go to the next loop to do the same with the next data set. The result object, even though it is dimmed inside the for each loop, is not being "newed" on each loop.
I've looked everywhere I can on the xbasic documentation but I can't find the area that tells me how to handle creating a new object on each loop, or creating a copy of the object inside the array, so that each index of the array has the correct information. Currently it is just storing a reference to the result object in each index so each index has the same information (the last loop of the array's information). I am going to use this array of objects with json_generate to pass the information on afterwords.
I've added something very similar to the code here:
I feel like this should be easy, and it probably is, I just can't seem to find the information I'm looking for. Any help you may have is much appreciated.
I'm having some trouble with Xbasic and creating an array of objects to store data.
I have a for each loop, and within that loop I dim result as p. I store information in that result object, add it to a dynamic array, and go to the next loop to do the same with the next data set. The result object, even though it is dimmed inside the for each loop, is not being "newed" on each loop.
I've looked everywhere I can on the xbasic documentation but I can't find the area that tells me how to handle creating a new object on each loop, or creating a copy of the object inside the array, so that each index of the array has the correct information. Currently it is just storing a reference to the result object in each index so each index has the same information (the last loop of the array's information). I am going to use this array of objects with json_generate to pass the information on afterwords.
I've added something very similar to the code here:
Code:
for each assembly in assemblies 'PROBLEM - This result object is passed by reference not value. It is overwriting the values in the array after each iteration. Need to new up an object on each iteration, or figure out another method dim result as P result.assemblyId = assembly.id result.code = assembly.code 'Get new cost of the assembly result.newCost = calculateAssemblyCost(assembly) 'Get the current cost of the assembly dim sqlData as P sqlData = json_parse(SQL_getProduct(assembly.code)) dim cost as C cost = sqlData [1].cost result.currentCost = convert_type(cost, "N") 'Add the cost comparison object into the dynamic array to be reviewed by someone and confirmed to push changes. assemblyPrices[] = result next
Comment