PDA

Click to See Complete Forum and Search --> : Constructor chaining, I guess...


crptcblade
May 29th, 2003, 04:25 PM
I remember in Java, being able to do something like this...

public class SomeClass
{
public SomeClass() { this(0); }
public SomeClass(int x) { this(x,""); }
public SomeClass(int x, string s) { /* ... */ }
}


And that would work its way down the chain of constructors so that you didn't need a lot of repeat initializing code. That may not be the completely correct syntax, but I hope you get the idea.

Anyway, this doesn't seem to work in C#. Is there any way to do something like the above in C#?

:)

hellswraith
May 29th, 2003, 04:32 PM
Try this:

public class SomeClass
{
public SomeClass(): this(0)
public SomeClass(int x): this(x, "")
public SomeClass(int x, string s) { /* ... */ }
}

crptcblade
May 29th, 2003, 04:48 PM
Thanks.

:)