Mybase.New() in VS 2005

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In VS 2003, if I overloaded the New() method of a WindowsForm, I always
included a call to Mybase.New(), the same way this method was called in the
native New() method.

In VS 2005, the New() method in WindowsForms are not visible in the .vb or
Designer.vb files. If you add a New() method to the .vb file of the
WindowsForm, a formatted Sub is created for you, however, it is missing the
call to Mybase.New().

If I create WindowsForms that require an overloaded New(), should I add the
call to Mybase.New()? It seems logical that if you create a new WindowsForm
that Inherits System.Windows.Forms.Form, you should call the constructor of
its base class.
 
Hi Michael,

Thanks for your post.

Below is the VB.net syntax in MSDN for MyBase.New() method:
"In a derived class, every constructor must call a base class constructor
(MyBase.New). If the base class has a constructor with no parameters that
is accessible to derived classes, MyBase.New can be called automatically.
If not, a base class constructor must be called with parameters, and this
cannot be done automatically. In this case, the first statement of every
derived class constructor must call a parameterized constructor on the base
class, or call another constructor in the derived class that makes a base
class constructor call."

So, MyBase.New() is only needed if we want to call base class's
parameterized constructor. If we just want to live with emptry parameter
constructor, MyBase.New is no need, compiler will generate code to call it
automatically.

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thanks Jeffrey!

Very convenient, though I wouldn't mind seeing the method to remind me. If a
base class has only parameterized constructors, and a call to the base
constructor is left out in a derived class, I guess that you wouldn't see the
error until compile time.
 
Hi Michael,

Thanks for your feedback.

Yes, in this scenario, we won't see the error until in compile time. Ok, if
you have any further concern on this issue, please feel free to tell me,
thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top