Colette

AFAttribute value disappears after changing template to one that is derived from the original template

Discussion created by Colette on Feb 15, 2013
Latest reply on Feb 18, 2013 by hanyong

Hello VCampus,

 

I am programmatically changing the template of an AFElement from it's original template to a diffent template that is derived from the original template. I assumed that the value of the AFAttribute of type string, that is an attribute common to both the base and derived template would persist after the template was changed, however it does not. It's not a big deal. I work-around the problem by copying the attribute value before the template is changed and restoring it after the template is changed.

 

I am using AFSDK version 2.5.0.4979. A code sample to reproduce the problem is below.

 

Is the loss of the attribute value the intended behavior?

 

 

 

using System;

 

using System.Collections.Generic;

 

 using System.Linq; using System.Text;

 

using System.Threading.Tasks;

 

 using OSIsoft.AF;

 

using OSIsoft.AF.Asset;

 

namespace ResetTemplateTest

 

 {   

 

class Program   

 

{      

 

 static void Main(string[] args)       {

 

        string AFTestDatabaseName = "TemplateResetIssue";                 

 

        PISystems systems = new PISystems();        

 

        PISystem piSystem = systems.DefaultPISystem;

 

        AFDatabase afTestDatabase = piSystem.Databases[AFTestDatabaseName];        

 

        if (afTestDatabase != null)        

 

        {             piSystem.Databases.Remove(AFTestDatabaseName);         }

 

 

 

        afTestDatabase = piSystem.Databases.Add(AFTestDatabaseName);

 

        AFElementTemplate baseTemplate = afTestDatabase.ElementTemplates.Add("BaseTemplate");        

 

        AFAttributeTemplate attrForBaseTemplate = baseTemplate.AttributeTemplates.Add("BaseTemplateAttribute");        

 

        attrForBaseTemplate.Type = typeof(String);

 

        AFElement testElement = afTestDatabase.Elements.Add("testElement", baseTemplate);        

 

        AFValue afValue = new AFValue();        

 

        afValue.Value = "ValueAssignedWhenElementWasUsingBase";        

 

        testElement.Attributes["BaseTemplateAttribute"].SetValue(afValue);

 

        afTestDatabase.CheckIn();

 

        string attributeValue = testElement.Attributes["BaseTemplateAttribute"].GetValue().ToString();        

 

        Console.WriteLine("The value of the 'BaseTemplateAttribute' is " + attributeValue);

 

        AFElementTemplate derivedTemplate = afTestDatabase.ElementTemplates.Add("DerivedTemplate");        

 

        derivedTemplate.BaseTemplate = baseTemplate;        

 

        AFAttributeTemplate attrForDerivedTemplate = derivedTemplate.AttributeTemplates.Add("DerivedTemplateAttribute");        

 

        attrForDerivedTemplate.Type = typeof(String);

 

        testElement.Template = derivedTemplate;        

 

        AFValue afValueForDerivedAttr = new AFValue();        

 

        afValueForDerivedAttr.Value = "ValueAssignedWhenElementWasUsingDerivedTemplate";        

 

         testElement.Attributes["DerivedTemplateAttribute"].SetValue(afValueForDerivedAttr);

 

        afTestDatabase.CheckIn();

 

        attributeValue = testElement.Attributes["BaseTemplateAttribute"].GetValue().ToString();        

 

        Console.WriteLine("The value of the 'BaseTemplateAttribute' is " + attributeValue);

 

        attributeValue = testElement.Attributes["DerivedTemplateAttribute"].GetValue().ToString();        

 

        Console.WriteLine("The value of the 'DerivedTemplateAttribute' is " + attributeValue);

 

        Console.ReadLine();

 

        }    } }

Outcomes