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

C#

 
AnswerRe: interface implementation Pin
snorkie3-May-08 2:30
professionalsnorkie3-May-08 2:30 
GeneralRe: interface implementation Pin
George_George3-May-08 2:33
George_George3-May-08 2:33 
GeneralRe: interface implementation Pin
snorkie3-May-08 2:41
professionalsnorkie3-May-08 2:41 
GeneralRe: interface implementation Pin
George_George3-May-08 2:43
George_George3-May-08 2:43 
AnswerRe: interface implementation Pin
tgrt3-May-08 14:51
tgrt3-May-08 14:51 
GeneralRe: interface implementation Pin
George_George3-May-08 18:15
George_George3-May-08 18:15 
GeneralRe: interface implementation Pin
tgrt4-May-08 4:07
tgrt4-May-08 4:07 
QuestionA static riddle (or: how to avoid lazy instantiation?) Pin
Luca Leonardo Scorcia3-May-08 1:45
professionalLuca Leonardo Scorcia3-May-08 1:45 
Let's say I have a resource broker, a 'lookup resource' event and a main program that will invoke the lookup resource event. All of these reside in decoupled modules, there may be only one resource broker and of course only one lookup event definition.

I created the following classes to implement the situation above:

class Program
{
   static void Main(string[] args)
   {
       Console.WriteLine("Program::Main");
       EventBroker.Current.FireLookupResource(null, EventArgs.Empty);

       Console.ReadLine();
   }
}

public class EventBroker
{
    private static readonly EventBroker _current = new EventBroker();
    public static EventBroker Current { get { return _current; } }

    public event EventHandler LookupResource;

    private EventBroker() { }

    public void FireLookupResource(object sender, EventArgs args)
    {
        Console.WriteLine("EventBroker::FireLookupResource");

        if (LookupResource != null)
           LookupResource(sender, args);
    }
}

public class ResourceBroker
{
    private static readonly ResourceBroker _current = new ResourceBroker();
    public static ResourceBroker Current { get { return _current; } }

    private ResourceBroker()
    {
        Console.Write("ResourceBroker::ResourceBroker");

        EventBroker.Current.LookupResource += EventBroker_LookupResource;
    }

    private static void EventBroker_LookupResource(object sender, EventArgs e)
    {
        Console.Write("ResourceBroker::EventBroker_LookupResource");
    }
}

However the output is not what I expected: the LookupResource event never gets called. That's understandable, since the ResourceBroker never gets referred in the executing code, it will not get instantiated. But what should I do to get the 'correct' output?

Creating a NoOp method in the ResourceBroker, and calling it in the Main function, works but it seems to me like an ugly hack. Am I missing something in the language keywords/features?

Thanks in advance

Luca

The Price of Freedom is Eternal Vigilance. -- Wing Commander IV

En Það Besta Sem Guð Hefur Skapað, Er Nýr Dagur.
(But the best thing God has created, is a New Day.)
-- Sigur Ròs - Viðrar vel til loftárása

AnswerRe: A static riddle (or: how to avoid lazy instantiation?) Pin
carbon_golem3-May-08 4:15
carbon_golem3-May-08 4:15 
Questionopening pdf file in windows application using c# Pin
maruthi2-May-08 23:50
maruthi2-May-08 23:50 
AnswerRe: opening pdf file in windows application using c# Pin
Ed.Poore2-May-08 23:59
Ed.Poore2-May-08 23:59 
GeneralRe: opening pdf file in windows application using c# Pin
maruthi3-May-08 0:29
maruthi3-May-08 0:29 
GeneralRe: opening pdf file in windows application using c# Pin
Ed.Poore3-May-08 0:49
Ed.Poore3-May-08 0:49 
GeneralRe: opening pdf file in windows application using c# Pin
maruthi3-May-08 1:10
maruthi3-May-08 1:10 
GeneralRe: opening pdf file in windows application using c# Pin
Ed.Poore3-May-08 1:24
Ed.Poore3-May-08 1:24 
GeneralRe: opening pdf file in windows application using c# Pin
Ed.Poore3-May-08 1:26
Ed.Poore3-May-08 1:26 
AnswerRe: opening pdf file in windows application using c# Pin
Ed.Poore3-May-08 1:34
Ed.Poore3-May-08 1:34 
GeneralRe: opening pdf file in windows application using c# Pin
maruthi3-May-08 1:46
maruthi3-May-08 1:46 
QuestionHow to create new VPN Connection through C#.net Pin
Seema Gosain2-May-08 20:14
Seema Gosain2-May-08 20:14 
AnswerRe: How to create new VPN Connection through C#.net Pin
Christian Graus2-May-08 21:12
protectorChristian Graus2-May-08 21:12 
AnswerRe: How to create new VPN Connection through C#.net Pin
zeyad_aamer27-Jan-13 4:49
zeyad_aamer27-Jan-13 4:49 
Questionnot able 2 update in database Pin
sow_mj2-May-08 18:22
sow_mj2-May-08 18:22 
AnswerRe: not able 2 update in database Pin
Christian Graus2-May-08 21:10
protectorChristian Graus2-May-08 21:10 
QuestionSaving text manipulated data Pin
Casper Hansen2-May-08 17:13
Casper Hansen2-May-08 17:13 
AnswerRe: Saving text manipulated data PinPopular
Christian Graus2-May-08 17:22
protectorChristian Graus2-May-08 17:22 

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.