3

I know that you cannot override static functions, but for what I'm looking to do, it seems like the logical way to design my class.

I have a method that is associated with my abstract class, that i want to work without the class being instantiated, but will work differently on different implementations of that abstract class(all statically)

Usually when this happens I am overlooking something, or there is a better way to approach the design, so hopefully someone here can set me on the right path.

Essentially I have a Content abstract class something like:

public abstract class Content{
   public virtual void copyAll();
   public virtual void VerifyContent();
   public static virtual Dictionary<int,Content> ProcessAllFromDT(DataTable dt) //Cannot exist
}

And then an implementation of that Ringtone, and Wallpaper

public class Wallpaper:Content{
   public void CopyAll(){...}
   public void VerifyContent(){...}
   //Loads Wallpapers From DT
   public static Dictionary<int,Content> ProcessAllFromDT(DataTable dt){...} //content in this case is a wallpaper
}

Finally it would be nice to have something like:

public void ProcessAllContent<T>() where T:Content{
    ...
    var dictionary = T.ProcessAllFromDT(dt); // Doesn't compile.
}

Suggestions on a better design for what I'm trying to do here?

16
  • Both vb and c# I wrote my example in c#, but i will be using this information in both languages Commented Feb 10, 2015 at 14:59
  • If you need Content to be instantiable, why not just remove the abstract keyword? Commented Feb 10, 2015 at 15:06
  • No i dont want Content to be instantiable. The ProcessAllFromDT method is a static factory method. I just want that factory method to do different things in different subclasses. Specifically only in subclasses. Commented Feb 10, 2015 at 15:09
  • So what is the problem then? All of the code you posted should work, provided you instantiate T as a derived type (which you have to do anyway). Commented Feb 10, 2015 at 15:10
  • 1
    I take it back, I think you just want an extension method... maybe.. I have no idea what you're trying to do. Commented Feb 10, 2015 at 16:31

1 Answer 1

4

If I understand you right (and I don't think I do) then you might...

You could create a static member for a generic static class that has the function for each subclass type. Just make each sub class have a static constructor that sets the appropriate action for how it does processing.

public static class ProcessDt<T>
{
    public Action ProcessAllFromDt { get; set; }
}

public class ZuberfizzContainer : AbstractContainer
{
    static ZuberfizzContainer()
    {
        ProcessDt<ZuberfizzContainer>.ProcessAllFromDt = () => ZuberfizzContainer.StaticZuberfizzMethod();
    }
}

public class GinghamContainer : AbstractContainer
{
    static GinghamContainer()
    {
        ProcessDt<GinghamContainer>.ProcessAllFromDt = () => GinghamContainer.StaticGinghamMethod();
    }
}

then usage would be:

ProcessDt<ZuberfizzContainer>.ProcessAllFromDt(); // This will do the zuberfizz specific stuff
ProcessDt<GinghamContainer>.ProcessAllFromDt(); // This will do the gingham specific stuff
// or any other one that sets the processallfromdt method for its type
2
  • wow, when i thought i had no where to go, here is exactly what im looking for. Sorry if i was vague in my question, but you got it right sir! Commented Feb 10, 2015 at 17:32
  • 3
    @Ukemi yeah it was vague but the magic words were "switch on type" -> Remember this: Every time you want to write a predicate (if/switch/whatever) on a type, the entire purpose of the type system in .NET is to dispatch execution to various places based on types, so if you think it through you can almost always figure out how to make the .NET type system execute the conditional for you. So next time you want to do a predicate on a type, just think real hard about how to make the type system route your code flow for you instead. Commented Feb 10, 2015 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.