What does MyBase.New do?

calvincrumrine

New member
Joined
Dec 15, 2005
Messages
2
Location
Juneau, AK
Programming Experience
10+
I'm trying to learn VB.Net so I can modify an application for which we bought the source code.

Public Class Main Inherits System.Windows.Forms.Form
#Region "Variables"
Private Shared X as New XBase
Private Y as New YBase
#End Region
#Region "Windows Form Designer Generated Code"
Public Sub New()
MyBase.New()

In stepping thru the above execution goes from MyBase.New to Private Y as New YBase. Either MyBase.New does nothing (or at least nothing visible) or it's what is executing the variable declaration. In either case I don't understand why it skips the first declaration.

I'm confused.
 
MyBase.new() activates the new constructor of the base class. Basically it creates a new instance of the base class from which you inherit....Which would be the System.Windows.Forms.Form class because your class "Main" inherits it.
 
OK-I actually knew that but was looking for something more specific. As far as I can see, all it does in this instance is add a member to the Forms collection of the application-right?
 
Oh, sorry to be too general. To answer your question, yes you are correct, it is creating an instance of the forms class to your main class so it basically has everything its needs to operate to be a form just like the one in the original forms class.
 
Every class has to call its base class's constructor in order for the initialisation of that base class to take place. Otherwise your Main class would have to perform ALL the initialisation that the Form class and EVERY one of its ancestors performs. It doesn't do nothing. It's just that the debugger doesn't enter that code because it is compiled code that forms part of the .NET Framework. The reason your code goes from that to the declaration of the Y variable and skips the X variable is that X is Shared. There is a new Y variable created for every instance of the Main class, but because it is Shared there is only ever one Y variable that belongs to the class itself and not any particular instance. No Y variable is created so the debugger doesn't go there. The issue is that you obviously aren't aware of exactly what a Shared member is.
 
Back
Top