Showing results 1 to 3 of 3

Thread: VSTA in Addin Workspace

  1. #1
    Join Date
    27.10.2011
    Location
    Verona
    Posts
    29

    Default VSTA in Addin Workspace

    Hello,
    I'm following your VSTA Training course and moving my first steps in this type of programming interface. I've reachet lession unit 3.4.1 where it's created an external class from the "MyWorkspace.cs". the code in the class (called Creator) is the following:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace WorkspaceAddin
    {
        class Creator
        {
            public void CreateFrame(string frameName)
            {
                try
                {
    ///code here
    
                }
                catch (Exception ex)
                {
    
                }
            }
            public void CreateFrame(string frameName, int iTop, int iBottom, int iLeft, int iRight)
            {
                try
                {
    ///code here
    
                }
                catch (Exception ex)
                {
    
                }
            }
        }
    }

    then when I create the function in the MyWorkspace.cs I call:

    public void Macro_CreateFrame()        {
                Creator MyCreator = new Creator(this.ActiveDocument);
                MyCreator.CreateFrame("myFirstFrame");
                
            }
            public void Macro_CreateFrameWithPosition()
            {
                Creator myCreator = new Creator(this.ActiveDocument);
                myCreator.CreateFrame("MySecondFrame", 200, 600, 200, 600);
            }
    But when compiling I got always two errors:

    Code:
    Error    1    'WorkspaceAddin.Creator' does not contain a constructor that takes '1' arguments    C:\ProgramData\COPA-DATA\zenon760\VSTAWorkspace\CSharp\WorkspaceAddin\MyWorkspace.cs    192    33    WorkspaceAddin
    Where is my error? can you please help me?
    Then in the Creator.cs class I can't use Zenon references such as ITemplate or the MyProject reference.
    Any help is welcome, thanks!

  2. #2
    Join Date
    08.02.2017
    Posts
    12

    Default Re: VSTA in Addin Workspace

    Hi Luca,

    the problem here is, that inside of the "Creator" class, no constructor with 1 parameter exists.
    Per default, every class in C# has a constructor without parameters, which gets called whenever an instance of this class gets created. The default constructor always exists and does not need to be programmed.

    In your case, you want to forward the reference of a zenon project to your constructor class, which is done in the constructor (best practice). That's what you do here:
    Creator MyCreator = new Creator(this.ActiveDocument);

    The problem in your case is, the your "Creator" class does not know what to do with the further parameter, as the class only knows the default constructor.
    Means, you need to overload your constructor. This could look the following way:

            public Creator(IProject zProject)
            {
                if (zProject == null)
                {
                    System.Diagnostics.Debug.Print("No zenon reference in Creator Constructor");
                    return;
                }
                myProject = zProject;
            }
     
    The overloading of the constructor (with similar code) should actually be mentioned in the training material. If not, please contact the training department of COPA-DATA.


    Thanks and all the best for your further VSTA courses 
    Christian


  3. #3
    Join Date
    27.10.2011
    Location
    Verona
    Posts
    29

    Default Re: VSTA in Addin Workspace

    Dear Chris,
    I've modified a little your code. Now it's like this:

    public class Creator
        {
            private IProject myProject;
            public Creator(IProject zProject)
            {
                if (zProject == null)
                {
                    System.Diagnostics.Debug.Print("No zenon reference is passed");
                }
                myProject = zProject;
            }


    In this way I've declared a private IProject variable myProject then overloaded as you suggested me the class and in the call of the class now I can use a parameter

    public void Macro_CreateFrameWithPosition()
            {
                Creator myCreator = new Creator(this.ActiveDocument);
                myCreator.CreateFrame("MySecondFrame", 200, 600, 200, 600);
            }



    As I wrote, I've been watching the video and there is no passage where it's explained this overload method of the class, this is why I've encountered this little problem.
    If this thing I've written (i mean the C# code) is right I go ahead with the course.
    Thanks and I wait for your reply.
    Luca

Similar Threads

  1. Assembly "Scada.AddIn.Contracts"
    By Ruebezaehler in forum Add-In Framework
    Replies: 7
    Last Post: 31st August 2017, 08:36
  2. Replies: 2
    Last Post: 25th August 2017, 11:10
  3. Read workspace name from VSTA
    By miguel.martinez in forum VSTA
    Replies: 0
    Last Post: 9th November 2016, 13:19
  4. Workspace Addin mit VB.Net
    By and12121 in forum VSTA
    Replies: 5
    Last Post: 26th February 2014, 10:55
  5. Error while loading the VSTA editor-addin
    By jjplaw in forum zenon Supervisor
    Replies: 1
    Last Post: 24th August 2011, 07:35

Tags for this Thread

Posting Rules

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •