Copy-Constructor and Assignment Operator

This object supports copy-construction and assignment. This is useful for an application that wants to easily capture fields on a host screen for later processing. Rather than allocate text buffers and copy the string contents of the field, the application can simply store the field in a private ECLField object. The stored copy retains all the function of an ECLField object including the field's text value, attributes, starting position, length, etc. For example, suppose an application wanted to capture the first input field of the screen. Table 1 shows two ways this could be accomplished.
Table 1. Copy-Construction and Assignment Examples
Save the field as a string Save the field as an ECLField object
#include "eclall.hpp"
 
{
   char *SavePtr;  // Ptr to saved string
   ECLPS Ps('A'); // PS object
   ECLFieldList *List;
   ECLField       *Fld;
 
   // Get fld list and rebuild it
   List = Ps->GetFieldList();
   List->Refresh();
 
   // See if there is an input field
   Fld = List->GetFirstField(GetUnmodified);
   if  (Fld !=NULL) {
      // Copy the field's text value
      SavePtr=malloc(Fld->Length() + 1);
      Fld->GetScreen(SavePtr, Fld->Length()+1);
  }
 
  // We now have captured the field text
 
#include "eclall.hpp"
 
{
   ECLField SaveFld;  // Saved field
   ECLPS Ps('A');      // PS object
   ECLFieldList *List;
   ECLField       *Fld;
 
   // Get fld list and rebuild it
   List = Ps->GetFieldList();
   List->Refresh();
 
   // See if there is an input field
   Fld = List->GetFirstField(GetUnmodified);
   if  (Fld !=NULL) {
      // Copy the field object
      SaveFld = *Fld;
  }
 
  // We now have captured the field text
  // including text, position, attrib
 
There are several advantages to using an ECLField object instead of a string to store a field:
  • The ECLField object does all storage management of the field's text buffer; the application does not have to allocate or free text buffers or calculate the size of the buffer required.
  • The saved field retains all of the characteristics of the original field including its attributes and starting position. All of the usual ECLField member functions can be used on the stored field except SetText(). Note that the stored field is a copy of the original — its values are not updated when the host screen changes or when the ECLFieldList::Refresh() function is called. As a result, the field can be stored and used later in the application.
Assignment operator overrides are also provided for character strings and long integer value types. These overrides make it easy to assign new string or numeric values to unprotected fields. For example, the following sets the first two input fields of the screen:
ECLField *Fld1; //Ptr to 1st unprotected field in field list
ECLField *Fld2; // PTR to 2nd unprotected field in field list
 
Fld1 = FieldList->GetFirstField(GetUnprotected);
Fld2 = FieldList->GetNextField(Fld1, GetUnprotected);
if ((Fld1 == NULL) || (Fld2 == NULL)) return;
 
*Fld1 = "Easy string assignment";
*Fld2 = 1087;
Notes:
  1. ECLField objects initialized by copy-construction or assignment are read-only copies of the original field object. The SetText() method is invalid for such an object and will cause an ECLErr exception to be thrown. Because the objects are copies, they are not updated or deleted when the original field object is updated or deleted. The application is responsible for deleting copies of field objects when they are no longer needed.
  2. Calling any method on an unitialized ECLField object will return undefined results.
  3. An ECLField object created by the application can be reassigned any number of times.
  4. Assignments can only be made from another ECLField object, a character string, or a long integer value. Assigning any other data type to an ECLField object is invalid.
  5. If an assignment is made to an ECLField object that currently is part of an ECLFieldList, the effect is to update only the field's text value. This is allowed only if the field object is an unprotected field. For example, the following will modify the 2nd input field of the screen by copying the value from the 1st input field:
    ECLField *Fld1;   // Ptr to 1st unprotected field in field list
    ECLField *Fld2;   // Ptr to 2nd unprotected field in field list
     
    Fld1 = FieldList->GetFirstField(GetUnprotected);
    Fld2 = FieldList->GetNextField(Fld1, GetUnprotected);
    if ((Fld1 == NULL) || (Fld2 == NULL)) return;
     
    // Update the 2nd input field using text from the first
    FLD2 = * Fld1;
     
    Because Fld2 is part of an ECLFieldList, the above assignment is identical to:
    { char temp[Fld1->GetLength()+1];
      Fld1->GetText(temp, Fld1->GetLength()+1);
      Fld2->SetText(temp);
      delete []temp;
    }
     

    Note that this will throw an ECLErr exception if Fld2 is protected. Also note that only the text of Fld2 is updated, not its attributes, position, or length.

  6. Assigning a string to a field object is equivalent to calling the SetText() method. You can also assign numeric values without first converting to strings:
    *Field = 1087;
    This is equivalent to converting the number to a string and then calling the SetText() method.