Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. static property with polymorphism

static property with polymorphism

Scheduled Pinned Locked Moved C#
csharpvisual-studioooptutorialquestion
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lukeer
    wrote on last edited by
    #1

    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

    P J L L 5 Replies Last reply
    0
    • L lukeer

      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

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      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.

      L 1 Reply Last reply
      0
      • P PIEBALDconsult

        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.

        L Offline
        L Offline
        lukeer
        wrote on last edited by
        #3

        PIEBALDconsult wrote:

        Do you want each child class to have its own icon to be shared among the instances of that child class?

        That's exactly what I'm trying.

        Ciao, luker

        1 Reply Last reply
        0
        • L lukeer

          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

          J Offline
          J Offline
          Jimmanuel
          wrote on last edited by
          #4

          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:

          1 Reply Last reply
          0
          • L lukeer

            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

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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]


            Getting an article published on CodeProject should be easier and faster for Bronze and Silver authors.


            1 Reply Last reply
            0
            • L lukeer

              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

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              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 ;
              }
              

              }

              L 1 Reply Last reply
              0
              • P PIEBALDconsult

                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 ;
                }
                

                }

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                What's up with the syntax colouring?

                L P 2 Replies Last reply
                0
                • L Lost User

                  What's up with the syntax colouring?

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  the PRE tags says lang="TEXT" which disables all syntax coloring. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  Getting an article published on CodeProject should be easier and faster for Bronze and Silver authors.


                  1 Reply Last reply
                  0
                  • L Lost User

                    What's up with the syntax colouring?

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    Syntax doesn't have colour.

                    1 Reply Last reply
                    0
                    • L lukeer

                      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

                      L Offline
                      L Offline
                      lukeer
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups