Add-in question

F

Fredrik Wahlgren

Hi

The question i'm about to ask is perhaps more of a C# question but since I
for some weird reson don't get access to all microsoft newsgroups, I will ak
here.

I have an xll which I want to convert to C#. It is quite simple and it must
be both a COM add-in and an automation add-in. It should only run under
Excel. All the logic is done in a C++/ATL dll. The add-in must therefore
create an instance of the dll when it's loaded and keep this IDispatch
pointer until it's unloaded. In my xll, the pointer was a global variable.
The functions will simply pass them on to the dll and reurn the result to
Excel, pretty much like the shim dll.

I now understand that C# doesn't allow global variables. My understanding is
that I need to put my globals in a class, and it should never go out of
scope. I guess a sensible place to create an instance of this class is in
OnStartupComplete. I guess I need a constructor that would create an
instance of my dll. When the add-in is unloaded, I want to do the C++
equivalent of Release on the interface. This probably means I have to do so
in the destructor. I need to make the IDispatch pointer available to all
classes.

The other idea that struck me was that maybe I should declare the pointer
within the namespace where all the IDTExtensibility2 functions reside.

Is this right? I'd appreciate any comments you have on this subject.

Best Regards,
Fredrik
 
E

Emmanuel

Hi Fredrik,

I can only answer for this part:
I now understand that C# doesn't allow global variables. My understanding
is
that I need to put my globals in a class, and it should never go out of
scope. I guess a sensible place to create an instance of this class is in
OnStartupComplete.

You could use class variables instead of instance variables. In this way you
can "emulate" the global variables and you don't have the scope problem.

Emmanuel
 
F

Fredrik Wahlgren

Hmm. I imagine one way of doing this would be to first make a VB.NET add-in
and then have it converted to C#. I have noticed two prducts that claim they
can do this. How is a class variable different from an instance variable?

/ Fredrik
 
E

Emmanuel

Hello Fredrik,

Class variables and class methods are declared inside a class like this:

public class MyMathClass
{
public static double pi = 3.14;

public static int Add( int a, int b)
{
return a + b;
}
}

The keyword "static" makes pi variable and Add() method a class variable and
method.
When you want to use pi or Add() you do not have to instantiate the class by
creating a new object
(in fact you cannot use pi or Add() from an object). On OnStartupComplete
you can assign the class variable a value and then don't care about the
scope of the variable.

You use them like this:

public static int Main()
{
// Using a class variable or property.
double x;
x = 3256 * MyMathClass.pi;

// Using a class method.
int result;
result = MyMathClass.Add(2, 3);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top