Click here to Skip to main content
15,887,338 members
Home / Discussions / C#
   

C#

 
QuestionStop files being deleted ? Pin
BuggingMe20-Mar-10 12:42
BuggingMe20-Mar-10 12:42 
AnswerRe: Stop files being deleted ? Pin
Not Active20-Mar-10 12:56
mentorNot Active20-Mar-10 12:56 
GeneralRe: Stop files being deleted ? Pin
BuggingMe20-Mar-10 13:09
BuggingMe20-Mar-10 13:09 
GeneralRe: Stop files being deleted ? Pin
Not Active20-Mar-10 13:36
mentorNot Active20-Mar-10 13:36 
GeneralRe: Stop files being deleted ? Pin
BuggingMe20-Mar-10 13:42
BuggingMe20-Mar-10 13:42 
Question.net create table of contents for Sql server report service 2008 Pin
jassiedog20-Mar-10 11:29
jassiedog20-Mar-10 11:29 
QuestionViewing SSL Certificate - C# Webbrowser Control PinPopular
Jon Braunsma20-Mar-10 5:44
Jon Braunsma20-Mar-10 5:44 
QuestionStatic Member and Inheritance Pin
Tony Richards20-Mar-10 4:54
Tony Richards20-Mar-10 4:54 
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 Frown | :( ) would be most appreciated.

Edit : Fixed the angle brackets for the Dictionary generics.
AnswerRe: Static Member and Inheritance Pin
Luc Pattyn20-Mar-10 5:28
sitebuilderLuc Pattyn20-Mar-10 5:28 
AnswerRe: Static Member and Inheritance Pin
Giorgi Dalakishvili20-Mar-10 6:16
mentorGiorgi Dalakishvili20-Mar-10 6:16 
AnswerRe: Static Member and Inheritance Pin
Bernhard Hiller21-Mar-10 21:42
Bernhard Hiller21-Mar-10 21:42 
QuestionAnimated GIF Pin
Joseph M. Ziemianski20-Mar-10 4:42
Joseph M. Ziemianski20-Mar-10 4:42 
AnswerRe: Animated GIF Pin
The Man from U.N.C.L.E.20-Mar-10 5:04
The Man from U.N.C.L.E.20-Mar-10 5:04 
GeneralRe: Animated GIF Pin
Joseph M. Ziemianski20-Mar-10 5:10
Joseph M. Ziemianski20-Mar-10 5:10 
Questionpopulate a specefic elemnt with attribute as column or tag [modified] Pin
Jassim Rahma20-Mar-10 4:23
Jassim Rahma20-Mar-10 4:23 
AnswerFormatting Pin
Not Active20-Mar-10 4:39
mentorNot Active20-Mar-10 4:39 
GeneralRe: Formatting Pin
Jassim Rahma20-Mar-10 4:42
Jassim Rahma20-Mar-10 4:42 
GeneralRe: Formatting Pin
Not Active20-Mar-10 6:54
mentorNot Active20-Mar-10 6:54 
AnswerRe: populate a specefic elemnt with attribute as column or tag Pin
Rod Kemp21-Mar-10 13:52
Rod Kemp21-Mar-10 13:52 
Questionproblem reading child by attribute Pin
Jassim Rahma20-Mar-10 4:02
Jassim Rahma20-Mar-10 4:02 
AnswerRe: problem reading child by attribute Pin
Not Active20-Mar-10 4:08
mentorNot Active20-Mar-10 4:08 
GeneralRe: problem reading child by attribute Pin
Jassim Rahma20-Mar-10 4:14
Jassim Rahma20-Mar-10 4:14 
GeneralRe: problem reading child by attribute Pin
Jassim Rahma20-Mar-10 4:16
Jassim Rahma20-Mar-10 4:16 
GeneralRe: problem reading child by attribute Pin
Not Active20-Mar-10 4:28
mentorNot Active20-Mar-10 4:28 
GeneralRe: problem reading child by attribute Pin
Jassim Rahma20-Mar-10 4:33
Jassim Rahma20-Mar-10 4:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.