Results 1 to 2 of 2

Thread: array / object as paramater

  1. #1
    Senior Member
    Join Date
    Oct 2012
    Posts
    239

    array / object as paramater

    I need solutions in which a function - in addition to its returned value - also returns the parameter values.
    In this case, in the current parameter list, we must put an @ sign in front of the name of the scalar parameter-variable, so that the value of the variable can be modified by the function. The new value can be assigned to this variable and/or in a sub-function. e.g:
    Code:
        parameters myVar
           myVar = m_var
    or
         =udf(@myVar)
    I don't know how the value of the parameter can be changed if it is an array or an object. In such cases, the parameter is passed by address, so there is no need for the @ sign. I don't know how the parameter value (content, size, structure) can be changed within the function - corresponding to the example above.


    I'm thinking of such possibilities (I don't know which ones are allowed and which ones are not), but there may be other solutions:
    Maybe someone can help?

    1. if the formal parameter is a static array

    Code:
    parameters myArray
    
    1/a: declare myArray[x,y]
    1/b: dimension myArry[x,y]
    1/c: myArray = m.m_array
    1/d: amembers(myarray,myObj) (or another Lianja function, e.g. astore(myArray, myString)
    1/e: udf(myArray,<params>) (a udf whose parameter is an array)
    1/f: myArray = udf(<params>) (a function that returns an array)
    2. if the formal parameter is an object (or dynamic array)

    Code:
    parameters myobj
    
    2/a: myobj = array() or myobj = object()
    2/b: myObj = m.Obj
    2/c: addproperty(myObj, cProp, xVar)
    2/d: removeproperty(myobj, cprop)
    2/e: udf(myobj,<params>)
    2/f: myObj = udf(<params>)

  2. #2
    Lianja Development Team barrymavin's Avatar
    Join Date
    Feb 2012
    Location
    UK, USA, Thailand
    Posts
    7,359
    Blog Entries
    22
    Run this code with the Lianja 8.0Beta103 build and study it. If you have issues with the pre-release beta versions please submit tickets. The forums are not the place to discuss issues with beta software. Please adhere to our forum policies.

    These tests which i wrote for you should answer all of your questions that you asked.

    @var is needed to pass variables by reference as you stated.
    Arrays and objects do not need to be passed as parameters using @ as they are always passed by reference. So the @ is optional.

    Code:
    proc atest()
    	param myarray, myarray2, myarray3, myarray4
    	declare myarray[2]
    	declare myarray2[2]
    	myarray3 = m.m_array
    	myobj = object("hello" => "world", "testing" => "again")
    	amembers(anarray, myobj)
    	? "after amembers"
    	? anarray
    	myarray4 = anarray
    	? "after amembers assignment"
    	? myarray4
    endproc
    
    
    proc atest2()
    	declare array xxx[2]
    	return xxx
    endproc
    
    
    proc otest2()
    	myobj = object("hello" => "world", "testing" => "again")
    	return myobj
    endproc
    
    
    proc otest()
    	param myarray, myarray2, myarray3, myarray4
    	myarray = object()
    	declare myarray2[2]
    	myarray3 = m.m_obj
    	myarray3[] = "myarray3"
    	myobj = object("a" => "a", "hello" => "world", "testing" => "again")
    	addproperty(myobj,"test","again")
    	addproperty(myobj,"test2","again")
    	removeproperty(myobj,"a")
    	amembers(anarray, myobj)
    	? "after amembers"
    	? anarray
    	myarray4 = anarray
    	? "after amembers assignment"
    	? myarray4
    endproc
    
    
    proc atest3
    	param barry
    	dimension barry[2]
    	barry[1] = "this has changed"
    endproc
    
    
    proc otest3
    	param barry
    	barry = object("hello","world")
    	? "otest3"
    	? barry
    endproc
    
    
    proc otest4
    	param barry
    	barry = object("hello","world")
    	? "otest4"
    	? barry
    	return barry
    endproc
    
    
    ? "testing arrays"
    public array m_array[2]
    private var1, var2, var3, var4
    atest(@var1, @var2, @var3, @var4)
    ? "var1"
    ? var1
    ? "var2"
    ? var2
    ? "var3"
    ? var3
    ? "var4"
    ? var4
    
    
    ? atest2()
    
    
    ? otest2()
    
    
    declare arrtest[1]
    
    
    atest3(arrtest)
    ? arrtest
    
    
    ? "testing objects"
    m_obj = object()
    private var1, var2, var3, var4
    otest(@var1, @var2, @var3, @var4)
    ? "var1:"+typeof(var1)
    ? var1
    ? "var2:"+typeof(var2)
    ? var2
    ? "var3:"+typeof(var3)
    ? var3
    ? "var4:"+typeof(var4)
    ? var4
    
    
    declare arrtest[1]
    
    
    otest3(arrtest)
    ? arrtest
    
    
    ? otest4(arrtest)
    
    
    release var1, var2, var3, var4
    private var1[1], var2[1], var3[1], var4[1]
    m_obj = object()
    otest(var1, var2, var3, var4)
    ? "var1:"+typeof(var1)
    ? var1
    ? "var2:"+typeof(var2)
    ? var2
    ? "var3:"+typeof(var3)
    ? var3
    ? "var4:"+typeof(var4)
    ? var4
    
    
    Principal developer of Lianja, Recital and other products

    Follow me on:

    Twitter: http://twitter.com/lianjaInc
    Facebook: http://www.facebook.com/LianjaInc
    LinkedIn: http://www.linkedin.com/in/barrymavin

Bookmarks

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
  •  
Journey into the Cloud
Join us