static property with polymorphism
-
Hello experts, imagine an abstract base class CBase and two derived classes CChildOne and CChildTwo. The children represent some kinds of devices. They inherit all the stuff they have in common from CBase. What I'm trying is to use CBase as often as possible to prevent lots of case differentiations. I need the class at hand to tell me which icon to draw for an instance of that class. I would like CBase to have a property DeviceIcon, which is static. That way I wouldn't have to create an instance every time I need the icon. And I also would like to override that property in CChildOne and CChildTwo to always get the correct icon. Visual Studio tells me that a property cannot use both abstract and static keywords. Anyone an idea how to get that going? Or will I have to bite the bullet and forget about the static part?
Ciao, luker
-
Hello experts, imagine an abstract base class CBase and two derived classes CChildOne and CChildTwo. The children represent some kinds of devices. They inherit all the stuff they have in common from CBase. What I'm trying is to use CBase as often as possible to prevent lots of case differentiations. I need the class at hand to tell me which icon to draw for an instance of that class. I would like CBase to have a property DeviceIcon, which is static. That way I wouldn't have to create an instance every time I need the icon. And I also would like to override that property in CChildOne and CChildTwo to always get the correct icon. Visual Studio tells me that a property cannot use both abstract and static keywords. Anyone an idea how to get that going? Or will I have to bite the bullet and forget about the static part?
Ciao, luker
lukeer wrote:
cannot use both abstract and static keywords
On the class or the field? Either way, that wouldn't make sense. An abstract class may have a static field, but it would be shared by all its children. Are you trying to avoid that? Do you want each child class to have its own icon to be shared among the instances of that child class? You could probably use a static Dictionary<Type,Icon> to do that. And there may be other techniques as well.
-
lukeer wrote:
cannot use both abstract and static keywords
On the class or the field? Either way, that wouldn't make sense. An abstract class may have a static field, but it would be shared by all its children. Are you trying to avoid that? Do you want each child class to have its own icon to be shared among the instances of that child class? You could probably use a static Dictionary<Type,Icon> to do that. And there may be other techniques as well.
-
Hello experts, imagine an abstract base class CBase and two derived classes CChildOne and CChildTwo. The children represent some kinds of devices. They inherit all the stuff they have in common from CBase. What I'm trying is to use CBase as often as possible to prevent lots of case differentiations. I need the class at hand to tell me which icon to draw for an instance of that class. I would like CBase to have a property DeviceIcon, which is static. That way I wouldn't have to create an instance every time I need the icon. And I also would like to override that property in CChildOne and CChildTwo to always get the correct icon. Visual Studio tells me that a property cannot use both abstract and static keywords. Anyone an idea how to get that going? Or will I have to bite the bullet and forget about the static part?
Ciao, luker
something like this:
abstract class BaseClass
{
protected static readonly Icon BaseIcon = new Icon("...");public abstract Icon DisplayIcon { get; }
}
class ChildClassOne : BaseClass
{
static readonly Icon ChildIcon = new Icon("...");public override Icon DisplayIcon { get { return ChildClassOne.ChildIcon; } }
}
class ChildClassTwo : BaseClass
{
public override Icon DisplayIcon
{
get { return BaseClass.BaseIcon; }
}
}:badger:
-
Hello experts, imagine an abstract base class CBase and two derived classes CChildOne and CChildTwo. The children represent some kinds of devices. They inherit all the stuff they have in common from CBase. What I'm trying is to use CBase as often as possible to prevent lots of case differentiations. I need the class at hand to tell me which icon to draw for an instance of that class. I would like CBase to have a property DeviceIcon, which is static. That way I wouldn't have to create an instance every time I need the icon. And I also would like to override that property in CChildOne and CChildTwo to always get the correct icon. Visual Studio tells me that a property cannot use both abstract and static keywords. Anyone an idea how to get that going? Or will I have to bite the bullet and forget about the static part?
Ciao, luker
Hi, I would use a static member holding the icon for the class, and an instance member holding the actual icon for the instance, like so:
public class Base {
private static Icon baseIcon=...;
protected Icon myIcon=baseIcon;
...
}public class Child : Base {
private static Icon childIcon=...;public Child() { myIcon=childIcon; }
...
}:)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
-
Hello experts, imagine an abstract base class CBase and two derived classes CChildOne and CChildTwo. The children represent some kinds of devices. They inherit all the stuff they have in common from CBase. What I'm trying is to use CBase as often as possible to prevent lots of case differentiations. I need the class at hand to tell me which icon to draw for an instance of that class. I would like CBase to have a property DeviceIcon, which is static. That way I wouldn't have to create an instance every time I need the icon. And I also would like to override that property in CChildOne and CChildTwo to always get the correct icon. Visual Studio tells me that a property cannot use both abstract and static keywords. Anyone an idea how to get that going? Or will I have to bite the bullet and forget about the static part?
Ciao, luker
Here's a way to do it with generics -- I used strings to save time working it up. (I think it's a bit kludgey.)
public class Base<T>
{
protected static string icon ;public string Icon { get { return ( icon ) ; } }
}
public class Child1 : Base<Child1>
{
static Child1
(
)
{
icon = "Child1" ;return ; }
}
public class Child2 : Base<Child2>
{
static Child2
(
)
{
icon = "Child2" ;return ; }
}
-
Here's a way to do it with generics -- I used strings to save time working it up. (I think it's a bit kludgey.)
public class Base<T>
{
protected static string icon ;public string Icon { get { return ( icon ) ; } }
}
public class Child1 : Base<Child1>
{
static Child1
(
)
{
icon = "Child1" ;return ; }
}
public class Child2 : Base<Child2>
{
static Child2
(
)
{
icon = "Child2" ;return ; }
}
-
the PRE tags says
lang="TEXT"
which disables all syntax coloring. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
-
Syntax doesn't have colour.
-
Hello experts, imagine an abstract base class CBase and two derived classes CChildOne and CChildTwo. The children represent some kinds of devices. They inherit all the stuff they have in common from CBase. What I'm trying is to use CBase as often as possible to prevent lots of case differentiations. I need the class at hand to tell me which icon to draw for an instance of that class. I would like CBase to have a property DeviceIcon, which is static. That way I wouldn't have to create an instance every time I need the icon. And I also would like to override that property in CChildOne and CChildTwo to always get the correct icon. Visual Studio tells me that a property cannot use both abstract and static keywords. Anyone an idea how to get that going? Or will I have to bite the bullet and forget about the static part?
Ciao, luker
Thanks a lot to everyone. I finally added a static icon variable to each of the child classes. The base class got an abstract Image GetDeviceIcon() method that each child class overrides returning its own child class icon. That way the icon is shared between all instances of a child class and can differ between sibling classes. The drawback is, I have to create an instance of each class once to display the possible icons. But as I said, it's only once. Thanks again, luker
Ciao, luker