List Info

Thread: C-Sharp (C#) Group: Re: virtual and override confusion




C-Sharp (C#) Group: Re: virtual and override confusion
user name
2006-01-02 20:07:08
Right,

that example was a very simplified version of the real
thing. Here's a
less-simplified -and hopefully more correct- version:

//--- Lfx.dll
namespace Lfx.Forms
{
  class Form: Windows.Forms.Form {...};
  class ListingForm: Lfx.Forms.Form {
    private void cmdCreate_Click(object sender,
System.EventArgs e)
    {
        this.Create();
    }
    public virtual void  Create() {...}
  };
}

//--- MyPlugin.dll
namespace MyPlugin {
  public class MyListingForm: Lfx.Forms.ListingForm {
    public override void  Create() {
      //Some code here
      return base.Create();
    }
  };
}

My application then creates a variable of type
Lfx.Forms.ListingForm
and assigns is to a new instance of MyPlugin.MyListingForm;

As you can see, the cmdCreate_Click event handler calls the
Create()
member, which is overridden. But instead the base class
Create() member
is called (i.e. "//Some code here" never gets
executed).

As I said, when I single-step cmdCreate_Click(), I can see
"this"
is a MyPlugin.MyListingForm, and still this.Create() calls
Lfx.Forms.ListingForm's Create().

This is part of a large app (65+ K lines) which worked ok
when it was a
single exe. Now I got it divided into a framework (Lfx), the
main app
and some external components (like MyPlugin).

BTW, I'm using VS2003.

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )