Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Derived class thinks my base class got deleted [duplicate]

I tried to make a derived WinForm class, but I have an issue: when I try to create my DerivedClass (via Add > New element > Derived Form), Visual studio tells me that the object does not exist

I'm sure the object at stake is BaseForm because I get the Setting Error message shows in the console. (my guess is because the Close()function is called )

public partial class BaseForm : Form
{
    Port port;

    public BaseForm()
    {
        InitializeComponent();
    }

    protected void Form1_Load(object sender, EventArgs e)
    {
        port = new Port();
        if (port.Port_Setting() != 0)
        {
            Console.WriteLine("Setting Error");
            Close();
        }
    }
}

For now, BaseForm is empty, just a form with no component added.

The only part used of my Port class so far is this, just initializing it for further use:

class Port
{
    protected SerialPort sp = new SerialPort();
    public int Port_Setting()
    {
        Console.WriteLine("Setting port");
        if (sp.IsOpen)
        {
            sp.Close();
        }

        sp.BaudRate = 38400;
        sp.DataBits = 8;
        sp.StopBits = System.IO.Ports.StopBits.One;
        sp.Parity = System.IO.Ports.Parity.None;
        sp.PortName = "COM3";

        bool Error = false;
        try
        {
            sp.Open();
        }
        catch (UnauthorizedAccessException) { Error = true; }
        catch (ArgumentException) { Error = true; }
        catch (System.IO.IOException) { Error = true; }
        if (Error)
        {
            return -1;
        }
        return 0;
    }
}
like image 206
Jacques R Avatar asked Jun 04 '21 10:06

Jacques R


People also ask

What will happen if we call a derived class function overridden through a base class object?

Suppose, the same function is defined in both the derived class and the based class. Now if we call this function using the object of the derived class, the function of the derived class is executed. This is known as function overriding in C++. The function in derived class overrides the function in base class.

Can base class call derived class function?

Even though the derived class can't call it in the base class, the base class can call it which effectively calls down to the (appropriate) derived class. And that's what the Template Method pattern is all about.

Can a derived class make a public base function private?

You can only change the access specifiers of base members the derived class would normally be able to access. Therefore, you can never change the access specifier of a base member from private to protected or public, because derived classes do not have access to private members of the base class.

Can we create derived class object from base?

Answer: No. In general, When we create object of a class, its reference is stored in stack memory and object is stored in heap and address of object is assigned to class reference.


Video Answer


1 Answers

Generating the derived class via the Visual Studio's interface (Add > New Element>Derived Form) –

Aha now things are getting more clear to me.

When you derive a form in Visual Studio, and you open the derived form in the designer, some code on the base form (the parent where you inherited from) will be executed.

I don't know all methods from the top of my head, but I believe that the load event for example will be executed in the base form when you load the inherited form in the designer at design time.

This is not a flaw from VS, this is by design. The idea is that when you create a custom user control, the code in the user control can also be executed in design time. And inheriting a form from another is somewhat the same thing for the designer.

What you can do to solve this is check in the code of the base form if you are on design time or not.

I have done this by adding this method on my base form:

 protected bool IsInDesignMode
 {
     get { return DesignMode || LicenseManager.UsageMode == LicenseUsageMode.Designtime; }
 }

you can use it like this

    private void FormBase_Load(object sender, EventArgs e)
    {
        if (IsInDesignMode == false)
        {
            // write code here that should only run at runtime
        }
    }
like image 187
GuidoG Avatar answered Oct 22 '22 09:10

GuidoG