Runtime components : Core components : Data elements : Tasks : Instantiating data elements
  
Instantiating data elements
To instantiate data elements, use definitions and code as shown in the following examples:
Example 1
Instantiate a keyed collection defined in the data file and assign a value to one of its elements, use the following definition and code:
Data definition:
<kColl id="coll1">
  <field id="field1"/>
  <field id="field2"/>
</kColl>
Code:
KeyedCollection elem1 = (KeyedCollection)
DataElement.readObject("coll1");
elem1.setValueAt("field2","Hello");
Example 2
Instantiate a keyed collection with an inner keyed collection defined in the data file and assign a value to one of the inner collection's elements.
Data definition
<kColl id="coll2">
  <field id="field12"/>
  <field id="field22"/>
</kColl>
<kColl id="coll1">
  <field id="field1"/>
  <field id="field2"/>
  <refData refId="coll2"/>
</kColl>
There are two possibilities in code:
KeyedCollection elem1 = (KeyedCollection)DataElement.readObject("coll1");
elem1.setValueAt("coll2.field22","Hello");
or:
elem1.setValueAt("*.field22","Hello");
Example 3
Instantiate an indexed collection defined in the data file and assign a value to its data fields.
Data definition
<iColl id="icoll1" size="3">
<field id="field1"/>
</iColl>
Code
IndexedCollection elem1 = (IndexedCollection)DataElement.readObject("icoll1");
elem1.setValueAt("0","Hello 0");
elem1.setValueAt("1","Hello 1");
elem1.setValueAt("2","Hello 2");
Example 4
Instantiate an indexed collection defined in the data file and assign a value to the second one of its data elements (a keyed collection).
Data definition
<kColl id="coll1">
<field id="field1"/>
<field id="field2"/>
</kColl>
<iColl id="icoll1" size="2">
<refData refId="coll1"/>
</iColl>
Code
IndexedCollection elem1 = (IndexedCollection)DataElement.readObject("icoll1");
elem1.setValueAt("1.field2","Hello 2");
Example 5
Create an indexed collection containing two instances of a data field using the default constructor.
IndexedCollection ic = new IndexedCollection();
ic.setName("anyName");
DataField df1 = new DataField();
DataField df2 = new DataField();
df1.setName("field1");
df1.setDescription("This is an example");
df2.setName("field1");
df2.setDescription("This is an example");
ic.addElement(df1);
ic.addElement(df2);
The result of this code is like having the following definition in the XML Data file:
<iColl id="anyName" size="2">
  <field id="field1" description="This is an example"/>;
</iColl>
and instantiating it with:
IndexedCollection ic = (IndexedCollection)DataElement.readObject("anyName");
Example 6
Instantiate a dynamic keyed collection defined with no elements in the data file and assign a value to two new elements.
Data definition
<kColl id="coll1" dynamic="true">
</kColl>
Code
KeyedCollection elem1 = (KeyedCollection)DataElement.readObject("coll1");
elem1.setValueAt("field1","Hello");
elem1.setValueAt("field2",null,"com.ibm.btt.base.KeyedCollection");
Note As the keyed collection does not contain the elements to be set, this code first creates the element named field1 of type DataField, and then sets its value to "Hello" and creates an element named field2 of type KeyedCollection.
Example 7
Instantiate a keyed collection with a parameter defined with the <param> subtag in the data file, assign a value to one of its elements, and get the value of the parameter.
Data definition
<kColl id="coll1">
<param id="kCollparameterName" value="kCollparameterValue"/>
<field id="field1"/>
<field id="field2"/>
</kColl>
Code
KeyedCollection elem1 = (KeyedCollection)DataElement.readObject("coll1");
elem1.setValueAt("field2","Hello");
String
parameterValue=(String)elem1.getParameter("kCollparameterName");
Example 8
Instantiate an indexed collection with param attributes defined in the data file and assign a value to one of its elements.
Note The field inside the iColl also has a param attribute.
Data definition
<iColl id="coll1">
  <param id="iCollparameterName" value="iCollparameterValue"/>
  <field id="field1">
    <param id="fieldparameterName" value="fieldparameterValue"/>
  </field>
</iColl>
Code
IndexedCollection elem1 = (IndexedCollection)DataElement.readObject("coll1");
elem1.setValueAt("field1","Hello");
Go up to
Tasks