[Mono-list] Re: Mono-list digest, Vol 1 #2179 - 14 msgs

l0ne l0ne@email.it
Sat, 20 Nov 2004 09:29:35 +0100


Il giorno 19/nov/04, alle 22:20, mono-list-request@lists.ximian.com ha 
scritto:

>> Hi folks,
>> I think I'm gonna ask a dumb question, but well, I've been crawling 
>> the
>> web for about an hour or so and didn't find the answer, so let's ask :
>>
>> Basically what I want is what can be done in Java the following way :
>>
>> public class MyClass
>> {
>> 	private String member;
>>
>> 	public MyClass ()
>> 	{
>> 		this("stuff");
>> 	}
>>
>> 	public MyClass (String s)
>> 	{
>> 		this.member = s;
>> 	}
>> }

Note that the this or super/base constructor call must be the first 
line of the constructor both in Java and C#. In Java, you can do as 
above, but:

	public MyClass(String s) {
		if (s.length() > 0) this(s, 123); // error: constructor call must be
										  // first statement!
	}

	public MyClass(String s, int b) { ... }

gives an error when running javac. C# merely makes this explicit by 
using the c++ish way of putting the base/this constructor call outside 
the method body.
Note that you CAN run functions BEFORE the constructor by having them 
as arguments to the this() or super/base() call, but I don't think the 
Whole Thing's elegant in the end. I'd stick to the private method way 
of doing things which Always Works.

	public MyClass(String s) {
		if (s.length() > 0) initWithStringAndInt(s, 123);
	}

	public MyClass(String s, int b) { initWithStringAndInt(s, 123) }

	protected initWithStringAndInt(String a, int b) { ... }

  - e.v.

 
 
 --
 Email.it, the professional e-mail, gratis per te: http://www.email.it/f
 
 Sponsor:
 La foto che hai sempre vicino a te... Stampala su un nostro gadget!
* Stelle & Strisce stampa quello che vuoi TU .... dove Vuoi Tu!!
* 
 Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=2116&d=20-11