-2

i am writing library, that can be used for with different database engines. Abstract class has abstract DbParameter constructor and uses classes from System.Data.Common.

Now i have sutch structure :

public abstract class ASqlWorker{

 abstract protected DbParameter DbParameterConstructor(String paramName, Object paramValue);

}

now, can i call abstract DbParameterConstructor from a static method of ASqlWorker?

Logicaly, i could make abstractMethod static (it creates instance of an inheriter of DbParameter from name and value and doesn't use fields of ASqlWorker), but it is not allowed in C#. btw, it is not allowed because of usage of non-realised static method can cause troubles. But in my case it won't, because my abstract method is protected.

i just want to write some implicit convertor from DbParameter to new DbParameter[1] to have more flexible interfaces.

obratim
  • 467
  • 5
  • 13
  • 2
    You need an instance to call non-static methods. – DavidG Nov 25 '14 at 17:21
  • 1
    You can't call an instance method from a static method, without a reference to an instance of the class. – Ben Robinson Nov 25 '14 at 17:21
  • 1
    Which derived class implementation do you expect to be used here? Your whole premise is that `ASQLWorker` doesn't know how to implement the method, and there could be many different derivisions. Which one do you want to be used? – Servy Nov 25 '14 at 17:23
  • by the way, abstract static method for interface is preview feature for c# 10 https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/static-abstract-interface-methods – obratim May 30 '22 at 10:42

2 Answers2

0

Your question assumes you could call a method on an abstract "instance" and we all know that is not possible, however if a subclass was defined and you were calling the abstract defined method on an instance of the concrete implementation of the abstract base class then the answer is yes. But the point here is you must call an instance method on an instance.

This is possible:

ASqlWorker a = new SubClassesASqlWorker();  //<-- assume you extending ASqlWorker and implemented it

This is not possible:

ASqlWorker a = new ASqlWorker();

You could define your static method with a parameter of the abstract type but you can only pass it objects that are fully implemented and a subclass of the abstract class.

T McKeown
  • 12,971
  • 1
  • 25
  • 32
  • you are right. The point was to create in ASqlWorker static methods, that generates DbConnection, and DbParameter. Then this methods are used in non-static methods of ASqlWorker to implement some ordinary logic. Inheritors of ASqlWorker implements only generaters, and I get portable library. – obratim Jan 21 '15 at 13:31
0

I did it!

lol, i implemented abstract static method in C# !

what did i:

  • created abstract class AbstractDbParameterConstructors, that don't contains any state, only abstract methods.
  • class ASqlWorker now gets generic, inhereted from AbstractDbParameterConstructors.

    public abstract partial class ASqlWorker<TPC>
                where TPC : AbstractDbParameterConstructors, new()
    { ... }
    
  • declared private static variable

    private static TPC generator = new TPC();
    

here both i have something like abstract static method, and i am protected from undesirable effects, from witch creators of standard were trying to protect me.

deatails : http://sourceforge.net/projects/sqlworker/

obratim
  • 467
  • 5
  • 13