Arrays Question



This post is to address a question posed in one of the comments on another post.  The answer is a little too in-depth for a comment reply.  Also, I had been wanting to blog more about arrays anyway.

Here is the question:

Hi i have seen your comment on Arrays.
i need your guidence on creating array and using it in Peoplecode App package.
I have some logic written in SQR. Same thing i want create in App Package which can be used.
Let me brief you my requirement.
i have several parameters to be stored in an array which i will get from different validations and SQL tables for employees.which i will store it in a array. After finishing validations for all the employees i have pass those employee id and details some other system so i want to replicate in App package using Array can you guide me how i can design it in App package.

I also decided to use my step-by-step that I just completed.  So, this answer will build on that post.

Listening to the requirements, I don’t think that we need to extend the array object like the post that this comment was on.  At most, I think we might want to create an employee object that will store all of the fields (or parameters as the requirements call them) needed.

So, I am creating a new Application Package called BLG_BLOGGING.  Then, I am inserting a new Application Class called “EmployeeObject”:

Then, I created three properties in that class to hold three parameters relating to an employee.  I did not specify the “get” or “set” keywords so I don’t have to create getter and setter methods.  This is the easiest way to add properties to a class because you don’t have to write any code for the properties.  Here is the code:

class EmployeeObject
   property string EmployeeID;
   property string FirstName;
   property string LastName;
end-class;

Then, in the Application Engine program, you have to import your new class.  This statement does nothing more than tell the program you are going to use this class later on.

import BLG_BLOGGING:EmployeeObject;

Here are the different variables that we will need.

Local File &log;
Local BLG_BLOGGING:EmployeeObject &emp;
Local array of BLG_BLOGGING:EmployeeObject &ary;
Local number &x;

This code creates an empty array.  We need to pass it a copy of the employee object just so it knows what data type will be stored in the array.  It will not actually store anything in the array at this point.

&emp = create BLG_BLOGGING:EmployeeObject();
&ary = CreateArrayRept(&emp, 0);

Here is where we load the first employee into the array.  We set all of the properties, and then, we use the push() method to insert it into the array.

&emp.EmployeeID = "001";
&emp.FirstName = "Bob";
&emp.LastName = "Tomato";
&ary.Push(&emp);
&log.WriteLine("Added Bob Tomato -- length = " | &ary.Len);

Then, we repeat that with another employee.

&emp = create BLG_BLOGGING:EmployeeObject();
&emp.EmployeeID = "002";
&emp.FirstName = "Larry";
&emp.LastName = "Cucumber";
&ary.Push(&emp);
&log.WriteLine("Added Larry Cucumber -- length = " | &ary.Len);

Finally, we add a third employee just to give us some data.

&emp = create BLG_BLOGGING:EmployeeObject();
&emp.EmployeeID = "003";
&emp.FirstName = "Lunt";
&emp.LastName = "Squash";
&ary.Push(&emp);
&log.WriteLine("Added Lunt Squash -- length = " | &ary.Len);

Then, we need to loop through the array to show what it contains.  The Get() method accesses one of the elements in the array.  It does not remove the element from the array.

&log.WriteLine("");
&log.WriteLine("Employees: ");
For &x = 1 To &ary.Len
     &emp = &ary.Get(&x);
     &log.WriteLine("  " | &emp.EmployeeID | ") " | &emp.LastName | ", " | &emp.FirstName);
End-For;

Final Solution

Here is the Application Package, Employee Object:

class EmployeeObject
   property string EmployeeID;
   property string FirstName;
   property string LastName;
end-class;

Here is the App Engine Program:

import BLG_BLOGGING:EmployeeObject;

Local File &log;
Local BLG_BLOGGING:EmployeeObject &emp;
Local array of BLG_BLOGGING:EmployeeObject &ary;
Local number &x;

&log = GetFile("c:\temp\log.txt", "W", "A", %FilePath_Absolute);

&emp = create BLG_BLOGGING:EmployeeObject();
&ary = CreateArrayRept(&emp, 0);

&emp.EmployeeID = "001";
&emp.FirstName = "Bob";
&emp.LastName = "Tomato";
&ary.Push(&emp);
&log.WriteLine("Added Bob Tomato -- length = " | &ary.Len);

&emp = create BLG_BLOGGING:EmployeeObject();
&emp.EmployeeID = "002";
&emp.FirstName = "Larry";
&emp.LastName = "Cucumber";
&ary.Push(&emp);
&log.WriteLine("Added Larry Cucumber -- length = " | &ary.Len);

&emp = create BLG_BLOGGING:EmployeeObject();
&emp.EmployeeID = "003";
&emp.FirstName = "Lunt";
&emp.LastName = "Squash";
&ary.Push(&emp);
&log.WriteLine("Added Lunt Squash -- length = " | &ary.Len);

&log.WriteLine("");
&log.WriteLine("Employees: ");
For &x = 1 To &ary.Len
     &emp = &ary.Get(&x);
     &log.WriteLine("  " | &emp.EmployeeID | ") " | &emp.LastName | ", " | &emp.FirstName);
End-For;

&log.Close();

Here is the final output:

Added Bob Tomato -- length = 1
Added Larry Cucumber -- length = 2
Added Lunt Squash -- length = 3

Employees:
  001) Tomato, Bob
  002) Cucumber, Larry
  003) Squash, Lunt

Share

1 Comment

  1. Jim Marion says:

    Well done. A common mistake is to use an array to represent an object. An array is a collection. Once you start coding in a manner that says the element in index #1 is an emplid and #2 is a name, you have created an object masquerading as an array. This is a terrible mistake. It can work, but is an easy way to introduce bugs.

1 Trackbacks

Leave a comment