Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Base Constructor is Not Getting Called

Tags:

c#

constructor

I am having an issue where the base constructor for a derived class is not getting executed. I have done this a hundred times and I cannot figure out for the life of me why the base constructor is not executing. I'm hoping that someone can find something simple that I am missing. An example of the code is below. Does anyone have any idea why my base constructor is not getting called first? I have other classes that are implemented in the same fashion and the base constructor is always called first.

if (item.GetType() == typeof(OtherChargeItem))
{
    OtherChargeItemAddUpdateTest test = new OtherChargeItemAddUpdateTest((OtherChargeItem)item);
    test.StartPosition = FormStartPosition.CenterParent;
    test.ShowDialog();
}

public OtherChargeItemAddUpdateTest()
{
    InitializeComponent();
}

public OtherChargeItemAddUpdateTest(OtherChargeItem item)
        : base()
{
    currentItem = item;
}
like image 655
Grasshopper Avatar asked Jun 18 '12 16:06

Grasshopper


1 Answers

It looks like you want to call the default constructor in the same class, not the base class, so InitializeComponent gets called when the second constructor gets called. Try this() instead of base().

like image 144
thecoop Avatar answered Oct 23 '22 17:10

thecoop