views:

179

answers:

5

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?

+2  A: 

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.

alexdej
new would hide the method, not override it...
Malfist
which is what you want -- in the absence of an instance, the caller needs to select which type's implementation to use.
alexdej
+4  A: 

I think what you want is the Factory Design Pattern.

Winston Smith
A: 

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();
    }
}
scottm
I understand this.
Malfist
A: 

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.

Bob
I know this...I'm asking how to create this type of behavior, I know it's not possible for this direct implementation.
Malfist
A: 

And thus was born the Factory Design Pattern.

Jim Mischel