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 Member and Inheritance

Static Member and Inheritance

Scheduled Pinned Locked Moved C#
questionoop
4 Posts 4 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.
  • T Offline
    T Offline
    Tony Richards
    wrote on last edited by
    #1

    Hi, I have an abstract class like this:

    public abstract class Component
    {
    private static readonly Dictionary<string, Type> RegisteredProperties
    = new Dictionary<string, Type>();

    public static void RegisterComponent(Type propertyType)
    {
        // Validation logic removed. nameAttribute.Name is a string value that
        // is unique to each propertyType.
        RegisteredProperties.Add(nameAttribute.Name, propertyType);
    }
    

    }

    I then derive a group of classes from Component, like so:

    public class StandardTimeComponent : Component
    {
    static StandardTimeComponent()
    {
    RegisterComponent(typeof(DateTime.StartProperty));
    RegisterComponent(typeof(TimeZone.OffsetToProperty));
    }
    }

    public class NonStandardTimeComponent : Component
    {
    static StandardTimeComponent()
    {
    RegisterComponent(typeof(DateTime.StartProperty));
    RegisterComponent(typeof(TimeZone.OffsetToProperty));
    }
    }

    I then discovered that if both classes are instantiated, I get an exception as the static constructor tries to add the same key to dictionary twice. The static field is shraed by both inheriting classes. Thinking about it, this makes sense. So, my question is, is there a way to define a Dictionary so that it is statically available in the Base class, but such that StandardTimeComponent.RegisteredProperties is different to NonStandardTimeComponent.RegisteredProperties? Any suggestions or suitable Google terms (I drew a blank :( ) would be most appreciated. Edit : Fixed the angle brackets for the Dictionary generics.

    L G B 3 Replies Last reply
    0
    • T Tony Richards

      Hi, I have an abstract class like this:

      public abstract class Component
      {
      private static readonly Dictionary<string, Type> RegisteredProperties
      = new Dictionary<string, Type>();

      public static void RegisterComponent(Type propertyType)
      {
          // Validation logic removed. nameAttribute.Name is a string value that
          // is unique to each propertyType.
          RegisteredProperties.Add(nameAttribute.Name, propertyType);
      }
      

      }

      I then derive a group of classes from Component, like so:

      public class StandardTimeComponent : Component
      {
      static StandardTimeComponent()
      {
      RegisterComponent(typeof(DateTime.StartProperty));
      RegisterComponent(typeof(TimeZone.OffsetToProperty));
      }
      }

      public class NonStandardTimeComponent : Component
      {
      static StandardTimeComponent()
      {
      RegisterComponent(typeof(DateTime.StartProperty));
      RegisterComponent(typeof(TimeZone.OffsetToProperty));
      }
      }

      I then discovered that if both classes are instantiated, I get an exception as the static constructor tries to add the same key to dictionary twice. The static field is shraed by both inheriting classes. Thinking about it, this makes sense. So, my question is, is there a way to define a Dictionary so that it is statically available in the Base class, but such that StandardTimeComponent.RegisteredProperties is different to NonStandardTimeComponent.RegisteredProperties? Any suggestions or suitable Google terms (I drew a blank :( ) would be most appreciated. Edit : Fixed the angle brackets for the Dictionary generics.

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

      what is static in a base (or abstract) class, is shared by all its instances, both direct and inherited instances. what is static in a derived class, is shared by instances of that class only, hence: - move your dictionary to each of the derived classes; - pass it on as a parameter to the methods that need it (RegisterComponent in the base class). :)

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


      I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


      1 Reply Last reply
      0
      • T Tony Richards

        Hi, I have an abstract class like this:

        public abstract class Component
        {
        private static readonly Dictionary<string, Type> RegisteredProperties
        = new Dictionary<string, Type>();

        public static void RegisterComponent(Type propertyType)
        {
            // Validation logic removed. nameAttribute.Name is a string value that
            // is unique to each propertyType.
            RegisteredProperties.Add(nameAttribute.Name, propertyType);
        }
        

        }

        I then derive a group of classes from Component, like so:

        public class StandardTimeComponent : Component
        {
        static StandardTimeComponent()
        {
        RegisterComponent(typeof(DateTime.StartProperty));
        RegisterComponent(typeof(TimeZone.OffsetToProperty));
        }
        }

        public class NonStandardTimeComponent : Component
        {
        static StandardTimeComponent()
        {
        RegisterComponent(typeof(DateTime.StartProperty));
        RegisterComponent(typeof(TimeZone.OffsetToProperty));
        }
        }

        I then discovered that if both classes are instantiated, I get an exception as the static constructor tries to add the same key to dictionary twice. The static field is shraed by both inheriting classes. Thinking about it, this makes sense. So, my question is, is there a way to define a Dictionary so that it is statically available in the Base class, but such that StandardTimeComponent.RegisteredProperties is different to NonStandardTimeComponent.RegisteredProperties? Any suggestions or suitable Google terms (I drew a blank :( ) would be most appreciated. Edit : Fixed the angle brackets for the Dictionary generics.

        G Offline
        G Offline
        Giorgi Dalakishvili
        wrote on last edited by
        #3

        Do you really need to be able to inherit from the base class? If not you can make it non-abstract and generic. That way any instance of the class with the same type will share same dictionary and every other type will have its own dictionary.

        Giorgi Dalakishvili #region signature My Articles Browsing xkcd in a windows 7 way[^] #endregion

        1 Reply Last reply
        0
        • T Tony Richards

          Hi, I have an abstract class like this:

          public abstract class Component
          {
          private static readonly Dictionary<string, Type> RegisteredProperties
          = new Dictionary<string, Type>();

          public static void RegisterComponent(Type propertyType)
          {
              // Validation logic removed. nameAttribute.Name is a string value that
              // is unique to each propertyType.
              RegisteredProperties.Add(nameAttribute.Name, propertyType);
          }
          

          }

          I then derive a group of classes from Component, like so:

          public class StandardTimeComponent : Component
          {
          static StandardTimeComponent()
          {
          RegisterComponent(typeof(DateTime.StartProperty));
          RegisterComponent(typeof(TimeZone.OffsetToProperty));
          }
          }

          public class NonStandardTimeComponent : Component
          {
          static StandardTimeComponent()
          {
          RegisterComponent(typeof(DateTime.StartProperty));
          RegisterComponent(typeof(TimeZone.OffsetToProperty));
          }
          }

          I then discovered that if both classes are instantiated, I get an exception as the static constructor tries to add the same key to dictionary twice. The static field is shraed by both inheriting classes. Thinking about it, this makes sense. So, my question is, is there a way to define a Dictionary so that it is statically available in the Base class, but such that StandardTimeComponent.RegisteredProperties is different to NonStandardTimeComponent.RegisteredProperties? Any suggestions or suitable Google terms (I drew a blank :( ) would be most appreciated. Edit : Fixed the angle brackets for the Dictionary generics.

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          The key is "nameAttribute.Name" which "is unique to each propertyType". Change it that it is unique to each combination of class and propertytype.

          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