Malfist
Malfist

Reputation: 31815

Static Abstract methods in C#

I know it's a tautology to have a static abstract method, but how can I do something like this:

Base, abstract class:

abstract class QTimerDatabaseObject {

    public static abstract QTimerDatabaseObject createFromQTimer(DataRow QTimerRow);
    public abstract void saveRow();
}

Sample Implementation (Inside a User class that extends the QTimerDatabaseObject):

    public static override QTimerDatabaseObject createFromQTimer(DataRow QTimerRow) {
        int ID = (int)QTimerRow["id"];

        string Username = QTimerRow["username"].ToString();
        string Init = (QTimerRow["init"] ?? "").ToString();
        string FirstName = (QTimerRow["FirstName"] ?? "").ToString();
        string MiddleInitial = (QTimerRow["Midinit"] ?? "").ToString();
        string LastName = (QTimerRow["Lastname"] ?? "").ToString();
        string Salutation = (QTimerRow["salutation"] ?? "").ToString();

        int RefNum = (int)(QTimerRow["refnum"] ?? -1);
        int Timestamp = (int)(QTimerRow["timestamp"] ?? -1);
        int DelCount = (int)(QTimerRow["delcount"] ?? 0);

        bool IsHidden = (bool)(QTimerRow["hidden"] ?? false);

        return new User(ID, Username, Init, FirstName, MiddleInitial, LastName, Salutation, RefNum, Timestamp, DelCount, IsHidden);
    }

How can I do something like that?

Upvotes: 1

Views: 1317

Answers (5)

alexdej
alexdej

Reputation: 3781

Use 'new' instead of 'override'.

Another option is to have an abstract Factory class that defines Create, and then override that in a derived Factory class.

Update: I'm not really sure what you're after here, but if you want to make it possible to 'plug' into the creation of this type, you might replace the 'abstract' method with a delegate, like so:

public static Func<DataRow, QTimerDatabaseObject> createFromQTimer { get; set; }

That way a consumer of the class can replace the implementation.

Upvotes: 2

scottm
scottm

Reputation: 28699

From MSDN:

Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object.

Since static is to declare a static member of the type, you cannot override a static method.

You could create an interface for the static method to return, then just cast to your specific type:

public interface IQTimerDatabaseObject
{
    //whatever implementation you need
}

abstract class QTimerDatabaseObject
{

    public static IQTimerDatabaseObject createFromQTimer(DataRow QTimerRow)
    {
        IQTimerDatabaseObject obj =  //some code to statisfy the interface
        return obj;
    }
    public abstract void saveRow();
}

class QTimer : QTimerDatabaseObject
{
    public new QTimer createFromQTimer(DataRow QTimerRow)
    {
        return QTimerDatabaseObject.createFromQTimer(QTimerRow) as QTimer;
    }

    public override void saveRow()
    {
        throw new NotImplementedException();
    }
}

Upvotes: 0

Jim Mischel
Jim Mischel

Reputation: 134125

And thus was born the Factory Design Pattern.

Upvotes: 1

Bob
Bob

Reputation: 99834

When you call a static method the call goes through the class. When you call a regular (instance method) the call goes through the instance of the class.

It doesn't make sense to have static and abstract on the same definition because they get applied differently.

If you just remove the static keyword you should be able to get the functionality that you want. But judging by the name of your method, the create part leads me to believe you want to create a Factory.

Upvotes: -1

Winston Smith
Winston Smith

Reputation: 21932

I think what you want is the Factory Design Pattern.

Upvotes: 4

Related Questions