Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I call a static method of a C# class from VBA via COM?

If I define a class in a C#/.NET class library, then by making it COM visible I can instantiate the class and call its methods from VBA using COM.

Is there any way to call the static methods of such a class from VBA?

like image 524
Gary McGill Avatar asked Jun 12 '14 20:06

Gary McGill


People also ask

Can I call a static function from another file in C?

Functions in C are global by default. To make them local to the file they are created, we use the keyword static before the function. Static functions can't be called from any other file except the file in which it is created. Using static function, we can reuse the same function name in different files.

Can we call a non-static method from a static method in C#?

We can call non-static method from static method by creating instance of class belongs to method, eg) main() method is also static method and we can call non-static method from main() method . Even private methods can be called from static methods with class instance. yes.

Are there static methods in C?

C # in TeluguA static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name.

How do you call a static method?

A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables. Since the static method refers to the class, the syntax to call or refer to a static method is: class name. method name.


1 Answers

COM does not support static methods, and instances of COM objects do not invoke static methods. Instead, set ComVisible(false) on your static method, then make an instance method to wrap it:

[ComVisible(true)]
public class Foo
{
    [ComVisible(false)]
    public static void Bar() {}

    public void BarInst()
    {
        Bar();
    }
}

Or just make the method instance instead of static and forget static all together.

You don't have to mark the static method as not visible to COM, however it satisfies some code analysis tools that would warn you about static methods on COM visible types, and makes it clear that the static method is not intended to be visible to COM.

like image 59
vcsjones Avatar answered Sep 21 '22 22:09

vcsjones