Click here to Skip to main content
15,910,303 members
Home / Discussions / C#
   

C#

 
AnswerRe: Simple C# logic traingle ?? Pin
OriginalGriff15-Apr-10 8:28
mveOriginalGriff15-Apr-10 8:28 
AnswerRe: Simple C# logic traingle ?? Pin
PIEBALDconsult15-Apr-10 9:31
mvePIEBALDconsult15-Apr-10 9:31 
GeneralRe: Simple C# logic traingle ?? Pin
Jaison V15-Apr-10 10:40
Jaison V15-Apr-10 10:40 
AnswerRe: Simple C# logic traingle ?? Pin
Jaison V15-Apr-10 11:21
Jaison V15-Apr-10 11:21 
GeneralRe: Simple C# logic traingle ?? Pin
harold aptroot15-Apr-10 12:11
harold aptroot15-Apr-10 12:11 
AnswerRe: Simple C# logic traingle ?? Pin
Ravi Bhavnani15-Apr-10 17:24
professionalRavi Bhavnani15-Apr-10 17:24 
QuestionRecover deleted files!!! Pin
muteb15-Apr-10 7:22
muteb15-Apr-10 7:22 
AnswerRe: Recover deleted files!!! Pin
Dave Kreskowiak15-Apr-10 8:11
mveDave Kreskowiak15-Apr-10 8:11 
GeneralRe: Recover deleted files!!! Pin
muteb15-Apr-10 16:58
muteb15-Apr-10 16:58 
GeneralRe: Recover deleted files!!! Pin
Dave Kreskowiak15-Apr-10 17:53
mveDave Kreskowiak15-Apr-10 17:53 
GeneralRe: Recover deleted files!!! Pin
Jason Ingram16-Jun-10 13:25
Jason Ingram16-Jun-10 13:25 
GeneralRe: Recover deleted files!!! Pin
Dave Kreskowiak16-Jun-10 14:33
mveDave Kreskowiak16-Jun-10 14:33 
Questionhow could I make a set up for my Win App project that have database in sql server 2005? Pin
ronakT15-Apr-10 6:47
ronakT15-Apr-10 6:47 
AnswerRe: how could I make a set up for my Win App project that have database in sql server 2005? Pin
Tarakeshwar Reddy15-Apr-10 6:58
professionalTarakeshwar Reddy15-Apr-10 6:58 
Questionconvert projet from vb6 to vb.net Pin
toto_201015-Apr-10 6:03
toto_201015-Apr-10 6:03 
AnswerRe: convert projet from vb6 to vb.net Pin
Keith Barrow15-Apr-10 6:10
professionalKeith Barrow15-Apr-10 6:10 
AnswerRe: convert projet from vb6 to vb.net Pin
Abhinav S15-Apr-10 6:14
Abhinav S15-Apr-10 6:14 
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

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.