Alpha Video Training
Results 1 to 3 of 3

Thread: Xbasic: trouble with objects and pass by reference.

  1. #1
    Member
    Real Name
    Nate Phillips
    Join Date
    Sep 2018
    Posts
    7

    Default Xbasic: trouble with objects and pass by reference.

    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:

    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
    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.
    Last edited by natephillips7; 05-09-2019 at 01:49 PM.

  2. #2
    "Certified" Alphaholic
    Real Name
    David Kates
    Join Date
    Apr 2008
    Location
    Unionville, ON
    Posts
    7,537

    Default Re: Xbasic: trouble with objects and pass by reference.

    I think this is what you're after. I don't think you need assemblyPrices. You can just work with result. Move the dim result as p outside your loop... and change it to...

    Code:
    dim result[0] as p
    Now you're working with a dot variable array. Then...

    Code:
    result[].assemblyId = assembly.id
    result[..].code = assembly.code
    To add the first item in the array... result[]
    To use that item just created in the array... result[..]

    In the end result will be an array with each member of the array having properties.

  3. #3
    Member
    Real Name
    Nate Phillips
    Join Date
    Sep 2018
    Posts
    7

    Default Re: Xbasic: trouble with objects and pass by reference.

    Hey David,

    You're awesome, that is exactly what I'm looking for. It's working perfectly!

    Thanks for the quick help!

Similar Threads

  1. trouble with global field reference in filter of append opperation
    By Freewilly3d in forum Alpha Five Version 11 - Desktop Applications
    Replies: 9
    Last Post: 08-17-2015, 05:27 AM
  2. Embedded Objects In Mobile App Causing Uncaught Reference Errors
    By Justin Taylor in forum Mobile & Browser Applications
    Replies: 4
    Last Post: 02-25-2015, 09:56 AM
  3. Trouble getting reference to parent grid field
    By rjackson@arach.net.au in forum Application Server Version 11 - Web/Browser Applications
    Replies: 7
    Last Post: 07-25-2012, 12:26 PM
  4. Replies: 3
    Last Post: 02-15-2012, 11:07 AM
  5. Having trouble locating objects on form
    By Greg Fong in forum Alpha Five Version 4
    Replies: 4
    Last Post: 08-14-2001, 01:30 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •