1. Reynard Harrison
  2. PowerBuilder
  3. Friday, 23 June 2023 16:02 PM UTC

Passing a structure to a function 

I am getting an error 


****Code****

s_lineitem lineitem

return_code = carrier_api_resp(lineitem)

***************

Error or warning 

Incompatible types in the assignment: structure, s_lineitem

picture of structure

the picture is a function accepting the structure which is not an issue.

 

Attachments (1)
Accepted Answer
Brad Mettee Accepted Answer Pending Moderation
  1. Saturday, 24 June 2023 19:49 PM UTC
  2. PowerBuilder
  3. # Permalink

There are several ways to use structures when calling a function or subroutine. One with a direct reference to the sent structure, the other with a copy of it.

- Function declaration implicitly makes structure being sent the same 'pointer' as what's been used when function is called
    function carrier_api_resp(ref s_lineitem as_li)

    s_lineitem lineitem
    return_code = carrier_api_resp(lineitem)

    In this case, lineitem, and as_li are identical pointers to the structure and no copy was created


- Function declaration is that it takes only the structure, but not automatically by reference. In this case, a copy of the structure is made and sent to the function. Modifying any data in the structure will be lost when function is complete (unless you return the structure and assign/use it in the calling code).

    function carrier_api_resp(s_lineitem as_li)

    s_lineitem lineitem
    return_code = carrier_api_resp(lineitem)

    lineitem and as_li are different values here because the point to two different sets of data.


- Function declaration same as prev item, but when called, has the REF statement used, so it's functionally identical to first usage above

    function carrier_api_resp(s_lineitem as_li)

    s_lineitem lineitem
    return_code = carrier_api_resp(ref lineitem)

    lineitem and as_li are same when used this way as well, but you always need to remember the "ref" to make sure you're sending the actual structure and not a copy.

 

The help files do contain this info, but it's kinda spread out. Hopefully this helps condense it some and make it more understandable.

 

Hope this helps.

Comment
There are no comments made yet.
John Fauss Accepted Answer Pending Moderation
  1. Friday, 23 June 2023 16:11 PM UTC
  2. PowerBuilder
  3. # 1

Hi, Reynard -

You cannot pass a generic/base structure, as it contains no members. The datatype of the method's argument must be the specific type of structure object (s_lineitem).

If your design requires a generic structure, consider creating a non-visual object that contains only public instance variables, as this acts like a structure. You can create a base "structure-like" NVO if needed, then inherit from it to create the descendant NVO's.

Best regards, John

Comment
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Friday, 23 June 2023 17:15 PM UTC
  2. PowerBuilder
  3. # 2

Hi Reyanrd;

  This works great but you need to use an object pointer instead. For example:

Note that Structure objects are automatically considered as "AutoInstantiated" objects and as such, any reference to these type of classes are *not* considered a pointer. HTH

Yakov and I were one of the last Sybase guys to teach the BOOA (Building Object Oriented Application" course that Sybase gave but was originally developed by the University of Texas. Sadly, this course (IMHO) should still be available as it's one of the "key" aspects of understanding & proper OO coding in PB. At the last "in person" Appeon Conference in one of my technical sessions I started asking some BOOA theory questions and no one in a room of 40+ could answer or knew the correct answer to any of my basic OO questions.  :-(

Regards ... Chris

Comment
  1. Benjamin Gaesslein
  2. Tuesday, 27 June 2023 09:45 AM UTC
That's a good tip, I wouldn't've thought of that.

Sometimes I wish there was a way to explicitly declare and use pointers in PB. It's never obvious what will happen with object references.
  1. Helpful
There are no comments made yet.
Reynard Harrison Accepted Answer Pending Moderation
  1. Friday, 23 June 2023 16:34 PM UTC
  2. PowerBuilder
  3. # 3

I am shocked this does not work in any other language this method works it seems like I have to make the structure global and then use it anywhere I want and then discard it. So even if I declare it populate the structure I still cannot pass the structure to a function populated wow.

Comment
  1. John Fauss
  2. Friday, 23 June 2023 17:21 PM UTC
You can pass a populated structure as an argument to a method. Each structure class is its own unique datatype, so if you pass a structure of type s_lineitem to a method, that method’s signature must also declare that argument as datatype s_lineitem… it cannot be a different structure class.
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.