[Mono-list] Calling a constructor from within another's code

Ian Norton inb at ncipher.com
Tue Nov 16 14:21:22 EST 2010


Hi,

using System;

namespace Thing
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			Console.WriteLine ("Hello World!");
		}
	}
	
	
	public class Circle : Shape {
	
		public Circle () : this( new int[]{ 1,2 } ){
		}
	
		private Circle ( int[] pair ) : base ( pair[0], pair[1] ) {
		}
	
	}
	
	public class Shape {
		public Shape ( int mass, int id )
		{
	
			Console.WriteLine( "mass {0} id {1}", mass, id  );
		}
	}
}


On Tue, 2010-11-16 at 18:35 +0000, Francisco M. Marzoa 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




More information about the Mono-list mailing list