Visual Studio - Using compiled static classes in client apps [duplicate] - c#

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?

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.

COM does not support static methods.
http://msdn.microsoft.com/en-us/library/ms182198.aspx

Related

How to make all methods from static class available in another static class in c#

I would like add additional c# methods to a external static class that can't be modified. i.e create a wrapper class for convenience containing the methods of the external static class + my own static methods.
I first wanted to inherit my own class from the external static class but I discover that static classes are sealed and can't be inherited form.
What would be the best way to do this?
Thanks a lot for you help
Proxy the calls, per Llama's indication
public static class YourClass{
public SomeType TheirMethod(..){
return TheirClass.TheirMethod(...);
}
...

Low-level difference: non-static class with static method vs. static class with static method

I was wondering what are the general benefits (or drawbacks) of using a non-static class with a static method versus a static class with the same static method, other than the fact that I cannot use static methods from a non-static class as extension methods.
For example:
class NonStaticClass
{
public static string GetData()
{
return "This was invoked from a non-static class.";
}
}
Versus this:
static class StaticClass
{
public static string GetData()
{
return "This was invoked from a static class.";
}
}
What are the performance/memory implications of using one method over another?
NOTE: Suppose that I do not need to instantiate the class. My use-case scenario is limited to something like this:
Console.WriteLine(NonStaticClass.GetData());
Console.WriteLine(StaticClass.GetData());
The main benefit is that if you make the class static, the compiler will make sure that your class will only have static members.
So anyone reading the code, will instantly see the class can't be instantiated, and there is no interaction to be considered with any instances of the class. Because there can't be any.
At the clr level, there is no notion of static. A static class is both abstract and sealed, which effectively prevents inheritance and instantiation.
As for performance, I don't see any possiblities for compiler or runtime to optimize one over the other.
In this example, I would focus on expressing your intent as clear as possible to the readers. You can always optimize later.
There are some peculiarity/limitation:
you can't instantiate static class
you can't inherit static class
So if you suppose such a behavior for you class (e.g. helper/util or extension methods container), if you want to limit it's usage - make it static.
There are no benefits or drawbacks.
It's just architectual decision.
Use static only classes in cases of providing a function set or function libriary.
As you can not instantiate it, but you can use it like.
MyMathLibrary.Plot(...)
This types are usually imply state less behavior. I repeat, usually.
Use simple classes with static members when you have "normal" type but need somehow a stateless method, like for example:
public class MyType
{
.... //some members
public static MyType ReadFromXml(XmlReader reader) {}
public static void SaveToXml(MyType mt) {}
}
There is no a silver bullet, it's just a matter of architect choice.
Performance implications: basically none.
You can create an instance of NonStaticClass, but since it has no non-static overriden methods it's pretty much as useful as saying object obj = new object(), which is to say it's useful for maybe locking and that's about it. Making the class static prevent someone from new-ing it, but it's not like that does any real harm.
It's more of a self documenting way of saying there's no reason to create an instance of the class.

can you have static and instance methods in the same c# or c++ class ?

Can you have static and instance methods in the same c# or c++ class ?
If yes, what would be the use of having both, if no why not ?
Yes, of course you can!
Static methods do not pass an implicit this pointer and are used whenever you do not need to operate on a specific instance of that class/type (such as modifying its members).
If you do need to modify a specific instance, you should use an instance method.
There's no reason that these two types of methods cannot be declared together in the same class. If you're using C#, check out some of the classes defined by the .NET Framework. Lots of them have both static and instance methods. That will give you some idea as to how these might be used effectively.
In C++, there's no reason to even create a "static" class (one with all static methods) as you might find in C#. The better approach would be to put the functions into a namespace as free functions. Not everything has to be an "object".
Yes you can.
And lots of uses. Static methods are usually library tools, which can be applied on instances of the class.
Instance methods are methods that you invoke by each specific instance.
Yes, here's a practicle example from the framework itself:
namespace System
{
public struct Int32 //...
{
public const int MaxValue = 2147483647;
//...
public TypeCode GetTypeCode();
public static int Parse(string s);
//...
}
}
Only those members are kept Static that are needed to be shared though each and every object of the same origin class equally, for instance if you have a static method (static method also requires to return a static value of the class), static members are initiated before even you make an object of that class.
now you also asked why and why not, remember there are thousand ways of doing so and thousand reasons if you start thinking in Object-Oriented manner.

Inheritance isn't working properly for generic abstract object factories

I have the following basic object factory for when I want some members of a class hierarchy to have special construction code and any other members to have generic constructors.
My problem here is that TileFactory doesn't have the method GetInstance--my program won't compile if I try to call TileFactory.GetInstance(). Any advice?
public static class ObjectFactory<K>
{
public static T GetInstance<T>() where T : K
{
T obj = (T)Activator.CreateInstance(typeof(T));
return obj;
}
//snip
}
}
//snip
public static class TileFactory : ObjectFactory<Tile>
{
}
Why can't I inherit static classes?
Citation from here:
This is actually by design. There seems to be no good reason to inherit a static class. It has public static members that you can always access via the class name itself. The only reasons I have seen for inheriting static stuff have been bad ones, such as saving a couple of characters of typing.
There may be reason to consider mechanisms to bring static members directly into scope (and we will in fact consider this after the Orcas product cycle), but static class inheritance is not the way to go: It is the wrong mechanism to use, and works only for static members that happen to reside in a static class.
(Mads Torgersen, C# Language PM)
Other opinions from channel9
Inheritance in .NET works only on instance base. Static methods are defined on the type level not on the instance level. That is why overriding doesn't work with static methods/properties/events...
Static methods are only held once in memory. There is no virtual table etc. that is created for them.
If you invoke an instance method in .NET, you always give it the current instance. This is hidden by the .NET runtime, but it happens. Each instance method has as first argument a pointer (reference) to the object that the method is run on. This doesn't happen with static methods (as they are defined on type level). How should the compiler decide to select the method to invoke?
(littleguru)
And as a valuable idea, littleguru has a partial "workaround" for this issue: the Singleton pattern.
http://www.dofactory.com/Patterns/PatternSingleton.aspx
There is no inheritance for static things. A workaround is to use singletons.

ASP.net c# publicly accessible functions in namespace

In my namespace I can have public classes which are accessible to all other pages in my namespace. How do I go about having public functions? Do I have to create a class with the functions in it? Or is there an easier way?
In C#, all functions (which are actually called methods in C# terminology) are to be declared in a type (a class or a struct).
However, there is the concept of static classes in C#, which are good for the purpose of replacing “global functions” in older programming languages:
public static class MyUtilityFunctions
{
public static void PrintHello()
{
Console.WriteLine("Hello World!");
}
}
Now you can, anywhere in your program, write:
MyUtilityFunctions.PrintHello();
A static method is a method that doesn’t require an object. A static class is a class that contains only static methods (and static fields, static properties etc.).
Functions (Methods) "live" in types, so you need to put them in classes or structs. By default they would be private so you need to specify the public access modifier for them to be accessible:
namespace myNameSpace
{
public class myClass
{
public void MyMethod()
{
}
}
}
See MSDN - Methods section of the C# programming guide.
When doing C#, functions can only live in types (classes and structures). You can not declare a function on its own.

Categories