can't use class my add-in

Latest post 01-07-2008 8:04 AM by Melody. 5 replies.
  • 11-23-2007 11:26 AM

    can't use class my add-in

    hello every one it's me again ..

    i created a windows application , windows application has got a class file (Report.cs)

     

    i created  add_in template  successfully but i can' t not create : Rapor rpr = new Rapor();    object

     

    when i build Add-in i got this exception : Error 1 No overload for method 'Rapor' takes '0' arguments .

    i checked my Report class but there is nothing wrong.

    can you help me ?

    here is my report class:

    public class Rapor

    {

    public Rapor ()

    {

    }

    public void CreateRapor(string formname)

    {

    System.Windows.Forms.Form frm = new System.Windows.Forms.Form();

    frm.Text = formname;

    frm.Show();

    }

     

    }

  • 11-23-2007 11:59 AM In reply to

    • Melody
    • Top 10 Contributor
    • Joined on 04-26-2007
    • Syracuse, NY
    • Posts 245

    Re: can't use class my add-in

    burakd,
       Unfortunately VSTA does not allow add-ins to use the "new" operator.  The easiest way to create a new object is to include an accessible method in your host that creates and returns a new object.  Below is a snippet from our EventSample that shows how to do this.

    I hope this helps.  Please let me know if you have anymore questions.
    -Melody


    I)  Declaring Objects in VSTA

    VSTA cannot access constructors from the host application.  The constructors included in the proxy file are for internal use only.  To create an object in VSTA, a public method must be available in the add-in entry point, in this case the MainApplication class, which constructs the object, then returns it.  The code below shows an object class, MainObject, the MainApplication class, and an add-in.  In the MainApplication class, the method NewObject is used by the add-in to create a MainObject in the add-in.

    //host application

    namespace Sample

    {

        //simple object used by main application

        public class MainObject

        {

            private string message;

            public string Message

            {

                get

                {

                    return message;

                }

            }

     

            public MainObject(string messageIn)

            {

                message = messageIn;

            }

        }

     

        //Main Application class

        public partial class MainApplication

        {

            public MainObject NewObject(String inMessage)

            {

                MainObject thisObject = new MainObject(inMessage);

                return thisObject;

            }

        }

    }

    //add-in

    namespace SampleProject1

    {

        public partial class AddIn

        {

            private void AddIn_Startup(object sender, EventArgs e)

            {

                //add a message to the display box

                EventSample.MainObject thisObject = this.NewObject("Add In- Object");

            }

        }

    }


  • 11-26-2007 9:31 AM In reply to

    Re: can't use class my add-in

    ok, it works ... thank you very much Melody ;

    my best regards...

    burak

  • 11-26-2007 9:41 AM In reply to

    • Melody
    • Top 10 Contributor
    • Joined on 04-26-2007
    • Syracuse, NY
    • Posts 245

    Re: can't use class my add-in

    No problem!
  • 01-06-2008 10:26 PM In reply to

    Re: can't use class my add-in

    oh my god, when will Microsoft solve this problem.we always need to new objects in my add-ins. 
    Filed under:
  • 01-07-2008 8:04 AM In reply to

    • Melody
    • Top 10 Contributor
    • Joined on 04-26-2007
    • Syracuse, NY
    • Posts 245

    Re: can't use class my add-in

    Microsoft has defended this design decision saying that objects are kept only on the host side so that VSTA add-ins do not have to do any "object managment".  Because this is a design decision Microsoft will not change this.  If you are interested in hearing more about this, check out the .NET Rocks show Naveen Yajaman on Visual Studio Tools for Applications.  That being said, it is possible to make modifications to allow the "new" constructor.

    -Melody

Page 1 of 1 (6 items) | RSS
Copyright Summit Software Company, 2008. All rights reserved.