Retrieving values from an Attribute

This section describes how to retrieve the value stored in an FndAttribute object. 

An FndAttribute object can be retrived from a FndDataRow with the string indexer. It takes a String containing the name of the attribute to get as it's argument.

Ifs.Fnd.Data.FndAttribute attr = row["ATTRNAME"];

To determine which kind of data the attribute holds, all record attributes have a data type. To find out the type for an attribute, use the GetAttributeType() method. It returns a FndAttributeType enumeration item.

All attributes contained in a FndDataRow are subclasses of Ifs.Fnd.Data.FndAttribute. The best way of working with attributes is to use type casting.

FndTextAttribute attr = (FndTextAttribute)row["DESCRIPTION"];

The table below shows which type of object the Attribute's GetValue() method returns for each of the data types.

AttributeType Attribute Class Value's class
FndAttributeType.Alpha FndAlphaAttribute string
FndAttributeType.Binary FndBinaryAttribute byte[]
FndAttributeType.Boolean FndBooleanAttribute bool
FndAttributeType.Date FndDateAttribute DateTime
FndAttributeType.Decimal FndDecimalAttribute double
FndAttributeType.Enumeration FndEnumerationAttribute string
FndAttributeType.Integer FndIntegerAttribute long
FndAttributeType.Number FndNumberAttribute double
FndAttributeType.Text FndTextAttribute string
FndAttributeType.Time FndTimeAttribute DateTime
FndAttributeType.TimeStamp FndTimeStampAttribute DateTime
FndAttributeType.Aggregate FndAggregateAttribute FndDataRow
FndAttributeType.Array FndArrayAttribute FndDataTable

An example

Imagine a record with the following structure (the @ sign is the divisor between the value and the domain):

<PERSON>
  FIRST_NAME:T=Homer
  LAST_NAME:T=Simpson
  AGE:I=38
  SPOUSE:AGG=Marge==>
    <PERSON>
      FIRST_NAME:T=Marge
  CHILDREN:ARRAY=
    <PERSON>
      FIRST_NAME:T=Bart
    <PERSON>
      FIRST_NAME:T=Lisa
    <PERSON>
      FIRST_NAME:T=Maggie 

The syntax for creating and reading the values is shown below:

// First create the meta data
FndDataColumnCollection cols = new FndDataColumnCollection("PERSON");
cols.Add("FIRST_NAME", FndAttributeType.Text);
cols.Add("LAST_NAME", FndAttributeType.Text);
cols.Add("AGE", FndAttributeType.Integer);
FndDataColumn col = cols.Add("SPOUSE", FndAttributeType.Aggregate);
col.ContainedRecordMeta = cols;
col = cols.Add("CHILDREN", FndAttributeType.Array);
col.ContainedRecordMeta = cols;

// Then create the data
FndDataRow row = new FndDataRow(cols);
((FndTextAttribute)row["FIRST_NAME"]).SetValue("Homer");
((FndTextAttribute)row["LAST_NAME"]).SetValue("Simpson");
((FndIntegerAttribute)row["AGE"]).SetValue(38);

FndDataRow spouseRow = new FndDataRow(cols);
((FndTextAttribute)spouseRow["FIRST_NAME"]).SetValue("Marge");
((FndAggregateAttribute)row["SPOUSE"]).SetValue(spouseRow);

FndDataTable childrenTable = ((FndArrayAttribute)row["CHILDREN"]).GetValue();

FndDataRow child = new FndDataRow(cols);
((FndTextAttribute)child["FIRST_NAME"]).SetValue("Bart");
childrenTable.Rows.Add(child);

child = new FndDataRow(cols);
((FndTextAttribute)child["FIRST_NAME"]).SetValue("Lisa");
childrenTable.Rows.Add(child);

child = new FndDataRow(cols);
((FndTextAttribute)child["FIRST_NAME"]).SetValue("Maggie");
childrenTable.Rows.Add(child);

// Trace data
Console.WriteLine("********* TRACE ************\n" + row.ToString() + "\n***************************\n");

// Then read the data
// 1. Get Homer
string name = ((FndTextAttribute)row["FIRST_NAME"]).GetValue();
Console.WriteLine("Name is " + name);

// 2. Get Marge
spouseRow = ((FndAggregateAttribute)row["SPOUSE"]).GetValue();
name = ((FndTextAttribute)spouseRow["FIRST_NAME"]).GetValue(); 
Console.WriteLine("Spouse is " + name);

// 3. Get Children
name = "";
childrenTable = ((FndArrayAttribute)row["CHILDREN"]).GetValue();
for(int i = 0; i < childrenTable.Rows.Count; i++)
{
    if(i > 0)
        name += ", ";
    name += ((FndTextAttribute)childrenTable.Rows[i]["FIRST_NAME"]).GetValue();
}
Console.WriteLine("Children are " + name);
The output from the code above will be:
********* TRACE ************
(
   $PERSON
      (

            (
               $FIRST_NAME:T/*=Homer
               $LAST_NAME:T/*=Simpson
               $AGE:I/*=38
               $SPOUSE:AGGREGATE/*
                  (

                        (
                           $FIRST_NAME:T/*=Marge
                        )
                  )
               $CHILDREN:ARRAY/*
                  (

                        (
                           $FIRST_NAME:T/*=Bart
                        )

                        (
                           $FIRST_NAME:T/*=Lisa
                        )

                        (
                           $FIRST_NAME:T/*=Maggie
                        )
                  )
            )
      )
)

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

Name is Homer
Spouse is Marge
Children are Bart, Lisa, Maggie