Krowi
Krowi

Reputation: 1615

Initializing 2 classes that have each other in their constructor

I have 2 classes, ExceptionHandler and Localizer. The ExceptionHandlers constructor looks like this:

public ExceptionHandler(ILocalizer localizer, string logLocation)
{
    Localizer = localizer;
    LogLocation = logLocation;
}

Further in the class the localizer is used to localize certain exception messages like "File not found" in English, "Bestand niet gevonden" in Dutch, ...

On the other hand, the Localizer class can have certain exceptions and therefore needs the ExceptionHandler class. The constructor looks like this:

public Localizer(IExceptionHandler exceptionHandler)
{
    ExceptionHandler = exceptionHandler;
}

Now, is it me or is what I want impossible to achieve? Is there any workaround to get it to work?

Upvotes: 0

Views: 65

Answers (1)

Dialecticus
Dialecticus

Reputation: 16761

You could set Localizer.ExceptionHandler after both Localizer and ExceptionHandler objects are created. Just make sure constructor does not throw exceptions, or the exceptions in the Localizer constructor are handled without ExceptionHandler

Upvotes: 2

Related Questions