[Mono-list] Calling a constructor from within another's code
Stifu
stifu at free.fr
Tue Nov 16 14:23:17 EST 2010
I don't think so.
It's the same in Java, the super() call must be the first instruction.
fran wrote:
>
> Hi Stifu,
>
> Thanks. Then I assume that there's no "official" manner of calling the
> base constructor within the new class one appart from the ": base
> (...)", isn't it?
>
> Regards,
>
>
> El 16/11/10 19:54, Stifu escribió:
>> You could always move the base constructor code to a protected method and
>> call it from the derived class constructor.
>>
>> public class C : B
>> {
>> public C ()
>> {
>> MyDataStruct data = GetDataFromTheDatabase ();
>> this.InitStuff( data.a, data.b );
>> }
>> }
>>
>> *shrugs*
>>
>>
>> fran wrote:
>>
>>> Hello,
>>>
>>> I know you can call a base class constructor using ": base" after the
>>> new constructor signature. I.e.:
>>>
>>> public class A
>>> {
>>> int i;
>>> public A (int _i)
>>> {
>>> i=_i;
>>> }
>>> }
>>>
>>> public class B : A
>>> {
>>> public B (int a, int b) : base (a+b) // Calls A constructor with _i
>>> as _a+_b
>>> {
>>> }
>>> }
>>>
>>> But what happens if you should made some more complicated that cannot be
>>> done in that way???
>>>
>>> For example:
>>>
>>> public class C : B
>>> {
>>> public C ()
>>> {
>>> MyDataStruct data = GetDataFromTheDatabase ();
>>> base ( data.a, data.b ); // This will not work, indeed, but
>>> illustrates the idea...
>>> }
>>> }
>>>
>>> This is a question more focused on C# than in mono itself, but I hope
>>> not to disturb no one doing it here since the list has not very high
>>> trafic.
>>>
>>> Best regards,
>>> _______________________________________________
>>> Mono-list maillist - Mono-list at lists.ximian.com
>>> http://lists.ximian.com/mailman/listinfo/mono-list
>>>
>>>
>>>
>>
>
> _______________________________________________
> Mono-list maillist - Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>
>
--
View this message in context: http://mono.1490590.n4.nabble.com/Calling-a-constructor-from-within-another-s-code-tp3045423p3045505.html
Sent from the Mono - General mailing list archive at Nabble.com.
More information about the Mono-list
mailing list