3 Replies Latest reply on Nov 7, 2016 1:23 PM by Marcos Vainer Loeff

    Navigating through AF Hierarchy using AF SDK

    natdavidson

      In developing some custom apps for our PI system, I keep running into the same question, what's the best way to navigate through an AF hierarchy in AF SDK?

       

      As an example, how i'm handling it now, I make a new AFElement myElement, set it equal to AFObject and use the FindObject method, pass in a (previously known) path and then assign that object as AF Element.

      From what i've been able to see, there isn't a clear way to Set a new AFElement to the element of another element (IE new AFElement is the child of another element).

       

      The only way I can think to navigate through a hierarchy is to use FindObject and use a previously known path.

       

      If this is it, that's fine, I just wanted to make sure I was using best practices.

        • Re: Navigating through AF Hierarchy using AF SDK
          PatrickCrummie

          There are a couple of ways,

          find the new element relative to the parent. This is especially useful for descendents:

          AFElement parent = (AFElement)AFObject.FindObject(PARENT_PATH);

          var el1 = (AFElement)AFObject.FindObject("ChildName", parent);

          var el2 = (AFElement)AFObject.FindObject(@"ChildName\GrandchildName", parent);


          Alternatively you can get a child element from the parent’s elements collection:

          AFElement parent = (AFElement)AFObject.FindObject(PARENT_PATH);

          var e3 = parent.Elements["ChildName"];

           

          You can also loop through the child elements:

          AFElement parent = (AFElement)AFObject.FindObject(PARENT_PATH);

           

          foreach (AFElement child in parent.Elements)

                 {

                    Console.WriteLine(child.Name);

                      }