|
||||||||
|
|
![]() |
|
|
|
|
||
|
Alternate Methods for Returning Data Structures from Subprocedures I have a concern with Steven Gray's technique [see "Returning a Pointer to a Data Structure from a Function"] using %STR to enable returning a pointer to a data structure: If there are UCS-2, packed, or integer fields in the data structure, it is highly probably that any fields following those fields will be skipped by %STR, because these data types frequently contain x'00' as part of their byte value. If a maintenance programmer unfamiliar with pointers added a subfield of one of those types, the mystery of the missing subfield data would be very difficult to solve. I think the calling interface would be just as simple if coded this way: C Callp getCustDS(zCustNum : CustDS) Here is the alternate procedure: P getCustDS B D getCustDS PI D zCustNum 05P 0 value D zDs likeds(CustDS) * C pCustNum Chain cfmt C If %found * load DS here C Eval zDs.CustNum = CustomerNo C Eval zDs.Name = %trim(FName) + ' ' + C %trim(MidInit) + ' ' + C %trim(LName) + ' ' C Eval zDs.Addr = address1 C Eval zDs.CityStZip = %trim(city) + ', ' C + %trim(state) + ' ' C + %trim(zipcode) C Eval zDs.CurBal = Balance C Eval zDs.CredBal = Credit - Balance C Else C Eval zDs.CustNum = *zeros C Endif P getCustDS E
--Barbara Morris Steven Gray responds: Barbara is correct. In my example the first hex 00's within the returned string will cause the %STR() function to stop copying data to the target DS. To prevent this, I should have pointed out that, when using this technique, the data structure should be made up of only CHAR and ZONED subfields. However, using a subprocedure this way puts the DS within the list of parameters, and I prefer to get my results as part of an EVAL statement whenever possible. If I modify Barbara's method slightly, I can still return the results with an EVAL. The keyword LIKEDS can be used in the procedure interface to let the compiler set up the returned DS. Here's how it works. First, here's the main body with the revised prototype and the revised call to the function statement: FCustomer IF E K DISK rename(Customer:Cfmt) * function prototype D getCustDS PR likeds( CustDS ) D pCustNum 05P 0 value * D CustDS DS qualified D CustNum 10S 0 D Name 40 D Addr 40 D CityStZip 40 D CurBal 9S 2 D CredBal 9S 2 * D zCustNum S 10S 0 * main C Eval zCustNum = 1001 * C Eval CustDS = getCustDS( zCustNum ) Notice the use of LIKEDS in the procedure interface to define the return value. Also, for readability, I include the QUALIFIED keyword when I define the first copy of the data structure. The call to the function is a simple EVAL statement, with my results on the left side and outbound parameters on the right. Next, this is what the procedure interface looks like: P getCustDS B D getCustDS PI likeds( CustDS ) D pCustNum 05P 0 value * D lds DS likeds( CustDS ) The use of the LIKEDS keyword will set up storage for the return data that matches the target DS. Actually, I could just say return a 148-byte-character field (and the compiler won't complain), but I want to avoid any hard-coded values. I also use LIKEDS to define a local DS for working storage. Then, within the body of the function, use the qualified naming format, dsl.subfield, when working with individual subfields in the DS. Whenever LIKEDS is used, you must use qualified naming to access subfields. Here's the body of the function: C pCustNum Chain cfmt C If %found C Eval lds.CustNum = CustomerNo C Eval lds.Name = %trim(FName) + ' ' + C %trim(MidInit) + ' ' + C %trim(LName) + ' ' C Eval lds.Addr = address1 C Eval lds.CityStZip = %trim(city) + ', ' + C %trim(state) + ' ' + C %trim(zipcode) C Eval lds.CurBal = Balance C Eval lds.CredBal = Credit - Balance C Else C Eval lds.CustNum = *zeros C Endif Last, the RETURN statement simply brings back the entire DS: C Return ( lds ) P getCustDS E Whichever way you prefer, both methods allow you to bundle complex data in a structure and to simplify the use of subprocedures. Steven Gray is a consultant with Paradigm Business Solutions, a Lawson and Microsoft business partner. Steven has worked with the AS/400 and iSeries (starting with the S/38) for over 16 years. E-mail: steven.gray@rcn.com
Editors: Howard Arner, Joe Hertvik, Ted Holt, David Morris
Managing Editor: Shannon Pastore
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message. |
|
| Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved. |