ZoolWay
ZoolWay

Reputation: 5505

Call constant property on class like static?

I got an abstract base class

public class Base
{
  public abstract String Info { get; }
}

and some children.

public class A : Base
{
  public override String Info { get { return "A does ..."; } }
}

public class B : Base
{
  public override String Info { get { return "B does ..."; } }
}

This is mere a constant but I want to make sure using Base that all classes implement it.

Now I sometimes do not have an object instance but want to access A.Info - this is not possible due it is a instance property.

Is there another way than implementing the same property on instance AND on static level? That would be feel like a duplicate violating DRY programming style.


NEW EDIT: I now see this two solutions:

public class Base
{
  public abstract String ClassInfo { get; }
}

public class A : Base
{
  public override String ClassInfo { get { return Info; } }

  public static String Info { get { return "A does ..."; } }
}

public class B : Base
{
  public override String ClassInfo { get { return Info; } }

  public static String Info { get { return "In B we do ..."; } }
}

With this I can do with any object of type Base something like object.ClassInfo but also use the value in my factory hardcoded like if(A.Info) return new A(). But I have to implement two properties for the same information in every class.

On the other hand:

public class Base
{
  public abstract String ClassInfo { get; }

  public static String GetClassInfo<T>() where T : BaseControl, new()
  {
    T obj = new T();
    return obj.ClassInfo;
  }
}

public class A : Base
{
  public override String ClassInfo { get { return "text A"; } }
}

public class B : Base
{
  public override String ClassInfo { get { return "text B"; } }    
}

Due to the abstract Base it is made sure that ClassInfo is always implemented. Calls with obj.ClassInfo and Base.GetClassInfo<A>() are okay. But with this every child of Base must have a default constructor without arguments and we loose performance with the unneccessary created instance.

Is there any other idea? Which one would you prefer and why?

Upvotes: 0

Views: 685

Answers (4)

Matthew Abbott
Matthew Abbott

Reputation: 61589

If you need specific return results of your static properties, you're better of either

a) Instance properties 2) Attributes

In the example you've already given, you've got an instance of Base, which means you can just make the instance property virtual:

public class Base
{
    public virtual string Info { get { return "From Base"; } }
}

public class A : Base
{
    public override string Info { get { return "From A"; } }
}

If you wanted to go the attribute route, you define it as such:

[AttributeUsage(AttributeTargets.Class, Inherited = true)]
public class InfoAttribute : Attribute
{
    public InfoAttribute(string info) { this.Info = info; }

    public string Info { get; private set; }
}

[InfoAttribute(Info = "From Base")]
public class Base
{
    public string GetInfo()
    {
        var attr = GetType()
            .GetCustomAttributes(typeof(InfoAttribute), true)
            .FirstOrDefault();

        return (attr == null) ? null : attr.Info;
    }
}

[InfoAttribute(Info = "From A")]
public class A : Base { }

If you wanted to call it as a static function call, you could make this change:

public static string GetInfo(Base instance)
{
    var attr = instance.GetType()
        .GetCustomAttributes(typeof(InfoAttribute), true)
        .FirstOrDefault();

    return (attr == null) ? null : attr.Info;
}

And then call it as: Base.GetInfo(instance);. All in all, not very elegant!

Upvotes: 5

Howard
Howard

Reputation: 3848

Does it compiled? I don't think so. Static cannot be marked as override, virtual or abstract.

Upvotes: 0

James Michael Hare
James Michael Hare

Reputation: 38397

Statics can't be overridden. If you truly want to do something like that, you'd want an instance property that is virtual in the base that gets overridden in the subclasses.

Upvotes: 0

SLaks
SLaks

Reputation: 887453

This is not possible.
static members cannot be virtual or abstract.

You should make an abstract instance property.

Upvotes: 3

Related Questions