O
Owen Blacker
Ok, dumb question, I know, but I just can't get my head round it.
I'm writing a base validator class, let's call it abstract BaseFoo.
I'm also writing two implementation classes, let's call them Bar and
Quux. I want them all to share a static field that defines a regular
expression pattern used in the validation. I fail to understand why,
logically, this cannot be so. (I'm entirely willing to accept (albeit
rather frustrated) that C# just doesn't allow it, it's the *rationale*
I'm having trouble getting my head round.)
Why can I not have something like:
public abstract class BaseFoo
{
public static abstract string MatchPattern { get; }
}
public class Bar : BaseFoo
{
public static override string MatchPattern { get { ... } }
}
etc?
I read a post on a Java forum[1] that almost got my head around it,
but now I no longer follow.
Sure, the static member in the abstract class would belong to that
class and not to the derived classes. But if it's abstract (rather
than virtual, though I think the logic would still stand for static
virtual members), what difference does it make?
Surely it makes more sense, in my example, for the MatchPattern --
which will not vary between instances of the Bar and Quux
implementations -- to be static, rather than to have to declare an
instance of Bar in order to reference its match pattern?
Anyone got any explanations that will make it all make sense to me?
Anyone got any cunning ploys that will allow me to have what is
effectively an abstract static member (and overridden static members
in the derived classes)?
All responses gratefully received,
Owen Blacker,
Technical Team Lead
I'm writing a base validator class, let's call it abstract BaseFoo.
I'm also writing two implementation classes, let's call them Bar and
Quux. I want them all to share a static field that defines a regular
expression pattern used in the validation. I fail to understand why,
logically, this cannot be so. (I'm entirely willing to accept (albeit
rather frustrated) that C# just doesn't allow it, it's the *rationale*
I'm having trouble getting my head round.)
Why can I not have something like:
public abstract class BaseFoo
{
public static abstract string MatchPattern { get; }
}
public class Bar : BaseFoo
{
public static override string MatchPattern { get { ... } }
}
etc?
I read a post on a Java forum[1] that almost got my head around it,
but now I no longer follow.
Sure, the static member in the abstract class would belong to that
class and not to the derived classes. But if it's abstract (rather
than virtual, though I think the logic would still stand for static
virtual members), what difference does it make?
Surely it makes more sense, in my example, for the MatchPattern --
which will not vary between instances of the Bar and Quux
implementations -- to be static, rather than to have to declare an
instance of Bar in order to reference its match pattern?
Anyone got any explanations that will make it all make sense to me?
Anyone got any cunning ploys that will allow me to have what is
effectively an abstract static member (and overridden static members
in the derived classes)?
All responses gratefully received,
Owen Blacker,
Technical Team Lead