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

C#

 
GeneralRe: convert projet from vb6 to vb.net Pin
toto_201015-Apr-10 6:46
toto_201015-Apr-10 6:46 
AnswerRe: convert projet from vb6 to vb.net Pin
William Winner15-Apr-10 6:17
William Winner15-Apr-10 6:17 
AnswerRe: convert projet from vb6 to vb.net Pin
Ian Shlasko15-Apr-10 6:44
Ian Shlasko15-Apr-10 6:44 
AnswerRe: convert projet from vb6 to vb.net Pin
nagendrathecoder15-Apr-10 19:02
nagendrathecoder15-Apr-10 19:02 
GeneralRe: convert projet from vb6 to vb.net Pin
toto_201016-Apr-10 0:23
toto_201016-Apr-10 0:23 
QuestionC# for BlackBerry Pin
Jassim Rahma15-Apr-10 5:53
Jassim Rahma15-Apr-10 5:53 
AnswerRe: C# for BlackBerry Pin
DaveyM6915-Apr-10 6:11
professionalDaveyM6915-Apr-10 6:11 
QuestionHow to force static instances of a class to initialize? [Solved] Pin
thugthug15-Apr-10 4:30
thugthug15-Apr-10 4:30 
I have a number of static instances of classes that do not get instantiated by the time I need them. These classes are loaded from external dll's, and may never be directly used. Here is an extremely simplified example.

A base class has a dictionary of all the instances of itself:

using System.Collections.Generic;

namespace Translators
{
    public class Translator
    {
        static public Dictionary<string, Translator> allTranslators = new Dictionary<string, Translator>();

        string language;

        public Translator(string language)
        {
            allTranslators.Add(language, this);
        }
    }
}


A derived class in another assembly has specific implementation of the base class

namespace Translators
{
    public class FrenchTranslator : Translator
    {
        private static FrenchTranslator TheFrenchTranslator = new FrenchTranslator();

        private FrenchTranslator()
            : base("French")
        {
        }

        public static void Initialize() { }
    }
}


Finally in a third assembly, a program, indirectly uses the derived class:
using Translators;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            Translator myTranslator = Translators.Translator.allTranslators["French"];
        }
    }
}


In the case above if you break in a and look at myTranslator you will find it null. Because even though the assembly containing FrenchTranslator has been loaded, the class has not been used yet.

However if you change Main to the following it will be properly assigned.

static void Main(string[] args)
{
    FrenchTranslator.Initialize();
    Translator myTranslator = Translators.Translator.allTranslators["French"];
}


Now, I know that the static members don't get initialized until the class is utilized, my question is ... is there a way to utilize the class when the assembly gets loaded so that the static instances will be available.

Please note that the example above is only an example and not the actual problem. Consider that in the real case there are many thousands of instances of the base type that have been created in over a dozen assemblies. And that these instances could be indirectly used from many clients in many assemblies / applications. Also it is a requirement that the instances remain static instances.

Any help or thoughts would be appreciated.

The Thug
modified on Friday, April 16, 2010 8:39 AM

AnswerRe: How to force static instances of a class to initialize? Pin
PIEBALDconsult15-Apr-10 4:50
mvePIEBALDconsult15-Apr-10 4:50 
QuestionRe: How to force static instances of a class to initialize? Pin
Keith Barrow15-Apr-10 5:09
professionalKeith Barrow15-Apr-10 5:09 
AnswerRe: How to force static instances of a class to initialize? Pin
thugthug15-Apr-10 6:13
thugthug15-Apr-10 6:13 
GeneralRe: How to force static instances of a class to initialize? Pin
Keith Barrow15-Apr-10 6:46
professionalKeith Barrow15-Apr-10 6:46 
GeneralRe: How to force static instances of a class to initialize? Pin
PIEBALDconsult15-Apr-10 7:45
mvePIEBALDconsult15-Apr-10 7:45 
GeneralRe: How to force static instances of a class to initialize? Pin
Keith Barrow15-Apr-10 10:10
professionalKeith Barrow15-Apr-10 10:10 
GeneralRe: How to force static instances of a class to initialize? Pin
PIEBALDconsult15-Apr-10 10:22
mvePIEBALDconsult15-Apr-10 10:22 
AnswerRe: How to force static instances of a class to initialize? Pin
PIEBALDconsult15-Apr-10 5:27
mvePIEBALDconsult15-Apr-10 5:27 
GeneralRe: How to force static instances of a class to initialize? Pin
thugthug15-Apr-10 6:18
thugthug15-Apr-10 6:18 
GeneralRe: How to force static instances of a class to initialize? Pin
PIEBALDconsult15-Apr-10 7:42
mvePIEBALDconsult15-Apr-10 7:42 
AnswerRe: How to force static instances of a class to initialize? Pin
Luc Pattyn15-Apr-10 6:13
sitebuilderLuc Pattyn15-Apr-10 6:13 
GeneralRe: How to force static instances of a class to initialize? Pin
PIEBALDconsult15-Apr-10 7:41
mvePIEBALDconsult15-Apr-10 7:41 
GeneralRe: How to force static instances of a class to initialize? Pin
Luc Pattyn15-Apr-10 7:47
sitebuilderLuc Pattyn15-Apr-10 7:47 
GeneralRe: How to force static instances of a class to initialize? Pin
PIEBALDconsult15-Apr-10 7:56
mvePIEBALDconsult15-Apr-10 7:56 
AnswerRe: How to force static instances of a class to initialize? Pin
thugthug15-Apr-10 8:36
thugthug15-Apr-10 8:36 
GeneralRe: How to force static instances of a class to initialize? Pin
Luc Pattyn15-Apr-10 9:06
sitebuilderLuc Pattyn15-Apr-10 9:06 
GeneralRe: How to force static instances of a class to initialize? Pin
PIEBALDconsult15-Apr-10 9:40
mvePIEBALDconsult15-Apr-10 9:40 

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.