8 Replies Latest reply on Mar 21, 2013 1:53 PM by dhollebeek

    Convertion of a Symbol to PBControl





      I'm getting an error when trying to convert a PBObjLib.Symbol to PBSymLib.PBControl in C#.


      The error says that the two types cannot be converted. Somebody knows what i'm missing?




      The Code  Example:

      m_display.Symbols.Add(PBObjLib.pbSYMBOLTYPE.pbSymbolControl, "myActiveXClass");
      foreach (Symbol s in m_display.Symbols)
            if (s.Type == (int) PBObjLib.pbSYMBOLTYPE.pbSymbolControl) 
                   myActiveXClassLib.myActiveXClass _axclass = (myActiveXClassLib.myActiveXClass) ((PBControl)s).Object;



      Thanks in Advance.

        • Re: Convertion of a Symbol to PBControl

          @Orlando: You need to test if you symbol is from the pbSymbolControl category, after you have to cast it into a PBSymLib.PBControl to be able to manipulate it. The PBControl object owns an Object property to manipulate the object from its real type. You need to test the type returned by the Object property before trying to cast it. Thereafter you can proceed with the newly casted object. I provided an example below to show you how.


          I hope it helped!

          // Make sure to reference the .dll/.tls file containing the ActiveX definition to your project.
               // You need to use the ProgID you have defined with the PhasorDiagram class you have created.          
               m_display.Symbols.Add(PBObjLib.pbSYMBOLTYPE.pbSymbolControl, "PhasorDiagramLib.PhasorDiagram");
               foreach(Symbol s in m_display.Symbols)
                    if(s.Type == PBObjLib.pbSYMBOLTYPE.pbSymbolControl)
                         // Cast you PBObjLib.Symbol object into a PBSymLib.PBControl               
                         PBSymLib.PBControl pbc = (PBSymLib.PBControl) s;
                         // Get access to your object through its type via the Object property.
                         // Validate if it is of type PhasorDiagramLib.PhasorDiagram
                         if(pbc.Object is PhasorDiagramLib.PhasorDiagram)
                              PhasorDiagramLib.PhasorDiagram diagram = (PhasorDiagramLib.PhasorDiagram) pbc.Object;
                              // Show creation date.
                         // Remove these comments to test for other type of controls.
                         // elseif(pbc.Object is ...)
                         // {
                         // }


            • Re: Convertion of a Symbol to PBControl

              Thanks Mathieu!


              I resolved the error by doing the casting (PBSymbol to PBControl) inside a VB.NET function.


              The cast to the PhasorDiagram don't  fully  work, it does the cast but don't  retrieve results from methods calls, only by doing .NET InvokeMember calls to access the  PhasorDiagram methods/properties.

                • Re: Convertion of a Symbol to PBControl

                  If you're using the latest C#, you can use a dynamic variable to cause the binding of method calls to be at run-time vs compile time.

                    • Re: Convertion of a Symbol to PBControl

                      Hi David,


                      Thanks for the tip, i've tried this code:

                      dynamic phasor = ((PBSymLib.PBControl)c).Object;
                      MessageBox.Show(((PhasorDiagramLib.PhasorDiagram) phasor).Created.ToString());

                       But get this by executing it:




                      As i said, by this code i have success by geting the Created property:

                      Type type = typeof(PhasorDiagramLib.PhasorDiagram);
                      Object o = type.InvokeMember("[DispID=65565]",
                      BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetProperty,

                      But i don't know why this approach works instead of the first one.