Вопрос

I am creating an accounting add in for Microsoft Excel using C#. In this add in, I want to exchange user data between individual forms. The closest thing I got was:

namespace ExcelAddIn
{
    public partial class ThisAddIn
    {
        public void RefreshExcelData()
        {
        }
    }
}

And...

private void btnUploadTestCases_Click(object sender, EventArgs e)
{
    var addIn = Globals.ThisAddIn;
    addIn.RefreshExcelData();           
}

This works to varying degrees of success, and it only allows me to call methods from that class. It doesn't allow me to access variables or class properties I instantiate within ThisAddIn.cs. What I need to know is how to initialize data (variables, properties, methods, etc.) such that it can be retrieved from any item in the solution, including my forms.

And for the love of God, keep it simple. I've spent the better part of a month on MSDN with far too much complications and little success.

Это было полезно?

Решение

First define a field in the addin (lets call it globalString):

namespace ExcelAddIn{       
    public partial class ThisAddIn{
        public static string globalString;
        public void RefreshExcelData(){
        }
    }
}

Then you can access the globalString variable from another class like this:

namespace ExcelAddIn{
   class TestClass {
      public static void SetTheGlobalStringValue(){
         ThisAddIn.globalString = "Global String";
      }
   }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top