Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Accessing overloaded constructors
Message
De
13/09/2004 23:55:30
 
 
À
13/09/2004 15:48:47
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00941066
Message ID:
00941781
Vues:
22
>Rick,
>
>Nope back at ya <g> ... that was the problem that Rex first posted about. >You get an error if you use base:

Well, your actually both right. But, Rex used the term "base constructor" which I think Rick assumed to mean the base classes (parent classes) constructor.

Using base() calls to the parent class constructor while this() calls the constructor in the same class. Here is the C# spec on this (10.10.1):

-------------

All instance constructors (except those for class object) implicitly include an invocation of another instance constructor immediately before the constructor-body. The constructor to implicitly invoke is determined by the constructor-initializer:

An instance constructor initializer of the form base(argument-listopt) causes an instance constructor from the direct base class to be invoked. That constructor is selected using argument-list and the overload resolution rules of Section 7.4.2. The set of candidate instance constructors consists of all accessible instance constructors contained in the direct base class (including any default constructor, as defined in Section 10.10.4). If this set is empty, or if a single best instance constructor cannot be identified, a compile-time error occurs.
An instance constructor initializer of the form this(argument-listopt) causes an instance constructor from the class itself to be invoked. The constructor is selected using argument-list and the overload resolution rules of Section 7.4.2. The set of candidate instance constructors consists of all accessible instance constructors declared in the class itself. If this set is empty, or if a single best instance constructor cannot be identified, a compile-time error occurs. If an instance constructor declaration includes a constructor initializer that invokes the constructor itself, a compile-time error occurs.
If an instance constructor has no constructor initializer, a constructor initializer of the form base() is implicitly provided. Thus, an instance constructor declaration of the form

C(...) {...}
is exactly equivalent to

C(...): base() {...}
The scope of the parameters given by the formal-parameter-list of an instance constructor declaration includes the constructor initializer of that declaration. Thus, a constructor initializer is permitted to access the parameters of the constructor. For example:

class A
{
public A(int x, int y) {}
}
class B: A
{
public B(int x, int y): base(x + y, x - y) {}
}
An instance constructor initializer cannot access the instance being created. Therefore it is a compile-time error to reference this in an argument expression of the constructor initializer, as is it a compile-time error for an argument expression to reference any instance member through a simple-name.

-------------

I think another thing that isn't always expected is that the construction-initializer (The part after the colon) runs BEFORE the constructor body. As specified in 10.10 "The optional constructor-initializer specifies another instance constructor to invoke before executing the statements given in the constructor-body of this instance constructor." However, if you think about it, this is what you want to happen.

Also, constructors are not inherited. So, if you have a class which is a subclass with constructor parameters, you have to specify that it calls the base class's constructor with parameters if that is what you want.

For example, lets say you subclass the ArrayList class.

class MyArrayList : System.Collections.ArrayList {}

You will notice when you try to use MyArrayList that it doesn't have any overloads where ArrayList has two plus the no parameter constructor? This is because the constructor was not inherited. So, what if we want to specify the capactity for our class? Well, we have to provide the override.

class MyArrayList : System.Collection.ArrayList
{
public MyArrayList(int Capacity) {}
}

Now, when you use your class, you see the overload, but if you look at the Capacity it is 16 which is the default. This is because since you didn't call a constructor-initializer the no parameter base construtor was called. So, your class actually looks like this after compiled:

class MyArrayList : System.Collection.ArrayList
{
public MyArrayList(int Capacity) : base() {}
}

To fix this, you have to call the correct base constructor. Also, we have to implement the no parameter constructor, because once we provide one the no parameter one isn't automatically created. Like this:

class MyArrayList : System.Collection.ArrayList
{
public MyArrayList() : base() {}
public MyArrayList(int Capacity) : base(Capacity) {}
}

----

HTH,
BOb
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform