1

I'm working on a WinForm application using ninject for dependency injection. My first problem was that the form being instantiated had a parameter (for DI). I added a parameterless constructor thinking this would help. The problem now is that the code inside the constructor with the parameter gets skipped. Here what it looks like:

On my main form:

private void mnuSettings_Click(object sender, System.EventArgs e)
{
   frmSettings objForm = new frmSettings();
   objForm.Owner=this;
   objForm.Show();
}

In the frmSettings form:

private readonly IApplicationPropertiesInterface _applicationProperties;

public frmSettings()
{
   InitializeComponent();
}

public frmSettings(IApplicationPropertiesInterface applicationProperties) : this()
{
   _applicationProperties = applicationProperties;
}

When I call _applicationProperties.GetExtractFileSaveLocationDirectory() it blows up because the code to set _applicationProperties was never called.

Im wondering if I have structured this incorrectly, and what the best way to achieve this is. My goal is to call the parameterless constructor, but also set _applicationProperties.

Any assistance would be most grateful.

1 Answers1

0

I'm guessing you might be expecting that having Ninject in the building will mena that new will work differently to normal. It doesn't - you need to be doing a kernel.Resolve<Something> for the DI to kick in. Note that most of these pitfalls are covered in detail on the wiki

Can you edit your answer to include details of what you're doing outside of this form please?

In the meantime, here are some previous questions that overlap significantly:-

Community
  • 1
  • 1
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249