简体   繁体   中英

C# Inherited Class looks for Base Class Functions

I have a couple different modeless forms that i recently inherited from a class i call Popup_Base. Basically, this just contains some events and variables that i use to get data back from a modeless form and allows me to pass a general calss to another helper class that i use to open these windows. Anyway, the problem is that for a particular Class that inherits off of this Popup_Base, it throws errors looking for Top Level functions in the Base Class. For example, a Calendar inherits off of the Popup_Base class and contains functions like Date_Click. However, the compiler throws an error saying that it cannot find it. Something like this:

"Error  1   'Popup_Base' does not contain a definition for 'monthCalendar1_DateSelected' and no extension method 'monthCalendar1_DateSelected' accepting a first argument of type 'Popup_Base' could be found (are you missing a using directive or an assembly reference?)"

I get a similar error

"Error  5   'Popup_Base.InitializeComponent()' is inaccessible due to its protection level

"

I wont post my entire code, but the Popup Class looks like this:

public partial class Popup_Base : Form
{
    public event EventHandler<NameUpdatedEventArgs> FirstNameUpdated;
    protected Control m_ctrl;
    protected virtual void OnFirstNameUpdated(NameUpdatedEventArgs e)
    {
        if (FirstNameUpdated != null)
            FirstNameUpdated(this, e);
    }

    protected int? m_row;
    protected int? m_col;

    public void Set_Sender_Info(Control Ctrl, int? Row = null, int? Col = null)
    {
        m_ctrl = Ctrl;
        m_row = Row;
        m_col = Col;
    }
}

(The names of this were taken from a tutorial)

Then, here is a sample calendar

public partial class form_Calendar : Popup_Base
{
    //
    public form_Calendar(int ix, int iy, Calendar_Display_Mode Type = Calendar_Display_Mode.Day, Control Sender = null, int? row = null, int? col = null)
    {
        if (Type == Calendar_Display_Mode.Month)
            //monthCalendar1.selection
            x = ix;
        y = iy;
        //this.Location = new Point(
        InitializeComponent();
        // this.Location = new Point(x, y);

    }
}

I feel like this is something really stupid that im missing.

I guess that is true, because your PopupBase indeed doesn't have a method called monthCalendar1_DateSelected . (It is most likely expected in the PopupBase.Designer.cs file. You must have double clicked the monthCalendar control and just removed the method, not the event handler registration.)

The error about InitializeComponent is probably true if you want to derive a class that is built with the designer. The InitializeComponent is most likely private in PopupBase , you have to make it protected to work, or only call the method from PopupBase , which seems to make more sense.

The type of the variable monthCalendar1 seems to be Popup_Base . And Popup_Base does not know anything about methods that are only present in derived classes. Imagine the following derived class:

public partial class form_Calendar : Popup_Base
{
    public void monthCalendar1_DateSelected(object sender, EventArgs e)
    {
    }
}

In that case you cannot call monthCalendar1_DateSelected on variables of type Popup_Base :

Popup_Base popup = new form_Calendar();
popup.DateSelected += popup.monthCalendar1_DateSelected; // <-- error!

You must call it on a variable of the derived type:

form_Calendar calendar = new form_Calendar();
calendar.DateSelected += calendar.monthCalendar1_DateSelected; // <-- this works!

If you have only a Popup_Base variable you can cast to the derived type:

Popup_Base popup= new form_Calendar();
form_Calendar calendar = (form_Calendar)popup;
calendar.DateSelected += calendar.monthCalendar1_DateSelected; // <-- this works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM