JTKH
JTKH

Reputation: 23

To enum or not to enum?

i'm trying to create an abstract class that encapsulates the basic features that i would like all tables to inherit from. i have a few hundred tables/views and would like to enforce some uniformity in the way the coding would look like across all classes. i know C# does not allow for an abstract enum, but is there a better way to do what i'm trying to achieve?

public abstract class basetbl
{
    public abstract enum efields;

    public virtual void doSort(params efields[] sortfields)
    {
        // some generic sort algorithm
    }
}
public class sometbl : basetbl
{
    public override enum efields
    {
        field1 = 0,
        field2 = 1,
        field3 = 2
    }
    public override void doSort(params efields[] sortfields)
    {
        // or some other code if the base algorithm is insufficient
    }
}
public class testenum
{
    ...
    public void dosort()
    {
        sometbl stbl = new sometbl();
        // get some data
        stbl.doSort(stbl.efields.field2, stbl.efields.field1);
        // do some stuff
        stbl.doSort(stbl.efields.field3);
    }
    ...
}

Upvotes: 2

Views: 886

Answers (1)

remio
remio

Reputation: 1280

Maybe Generics may help you. By declaring the base class as generic, you help developers to think about declaring the internal enum.

public class basetbl<TEnum>
{  
    public virtual void doSort(params TEnum[] sortfields)
    {
        // some generic sort algorithm
    }
}

public class sometbl : basetbl<sometbl.fields>
{
    public enum fields
    {
        field1 = 0,
        field2 = 1,
        field3 = 2
    }
    public override void  doSort(params fields[] sortfields)
    {
        // or some other code if the base algorithm is insufficient
    }
}

public class testenum
{
    ...
    public void dosort()
    {
        sometbl stbl = new sometbl();
        // get some data
        stbl.doSort(sometbl.fields.field2, sometbl.fields.field1);
        // do some stuff
        stbl.doSort(sometbl.fields.field3);
    }
    ...
}

Upvotes: 3

Related Questions