views:

92

answers:

6

Hi, I have just seen following code but I do not understand the derivation of base class right in the constructor declaration. What is this and is this possible with ordinal methods?

public SplashAppContext(Form mainForm, Form splashForm) : base(splashForm)
{
this.mainForm = mainForm;
splashTimer.Tick += new EventHandler(SplashTimeUp);
splashTimer.Interval = 2000;
splashTimer.Enabled = true;
}
+8  A: 

It's calling a base class constructor, passing the argument splashForm of the type Form to it.

You can call base class methods as well. If you for example have overridden a method which behaviour you want to modify slightly, you do your modifications and call the base class method with base.TheMethod(). This would look like this:

public override void FireMissiles()
{
   PrimeMissiles();

   base.FireMissiles();
}

The syntax for calling a base class constructor and a base class method differs as you can see.

Skurmedel
+4  A: 

The syntax you've listed means that this constructor calls the superclass's constructor with the parameter splashForm before continuing with the rest of the construction of this object. When no superclass constructor is listed, the no-arg constructor is assumed, i.e:

public SplashAppContext(Form mainForm, Form splashForm)
{ ... }

is equivalent to

public SplashAppContext(Form mainForm, Form splashForm) : base()
{ ... }
VeeArr
+2  A: 
: base(splashForm)

Is calling the parent constructor and passing splashForm to it.

Ian P
A: 

This is actually not really a case of inheritance, what this syntax specifies is that before SplashAppContext's constructor is called, its base constructor should be called with splashForm passed as a parameter.

Firoso
+4  A: 

What is this...

This line is called a constructor initializer. It means "call the base class's constructor with the parameter splashForm, then start this constructor's body".

When no constructor initializer is explicitly specified for a constructor, it's assumed that you want base(), the parameterless constructor.

...is this possible with ordinary methods?

For regular methods, you can of course call base class methods (with base.SomeMethod(...)), but there's no "initializer" style available.

John Feminella
+1  A: 

That is the syntax for calling the constructor of the immediate parent class. This is the C# syntax, whereas VB.NET requires that you call MyBase.New(splashForm) as the first line of code in your constructor.

This is expressed this way because a parent object is guaranteed to be fully constructed and initialized by the time your child constructor code begins to execute. Because of this, both compilers (VB.NET and C#) specify that you may not execute any of your own code before the parent constructor is called (if you don't specify a parent constructor and a parameterless constructor exists, it will be called automatically).

This syntax is not possible (nor necessary) for other method calls, as it's possible to invoke the base implementation of any other function by calling base.FunctionName() in your code. Constructors are just special cases because they have to execute before any other code.

Adam Robinson