Static Member and Inheritance
-
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.
-
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.
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.
-
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.
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
-
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.
The key is "nameAttribute.Name" which "is unique to each propertyType". Change it that it is unique to each combination of class and propertytype.