Collins, Michael G [mailto:[EMAIL PROTECTED]] wrote:
> Can this be done in C#? Or do I need to create 2 versions of > the function? Sure, you define multiple versions of the method with the same return type and varying parameter lists. This techinique is referred to as "overloading". If you want to, you can ultimately delegate to your longest parameter list version of the function with each smaller list delegating defaults to the next. Like so: <codeSnippet language="C#"> public void DoSomething() { this.DoSomething(1000); } public void DoSomething(int howLong) { this.DoSomething(howLong, true); } public void DoSomething(int howLong, bool sayHello) { if(sayHello) { Debug.WriteLine("Hello!"); } Thread.Sleep(howLong); } </codeSnippet> > If I have 3 different constructors, is it possible to call > one construtor from another? Or should I call a single init > routine from each constructor? Sure, you use the following syntax to do this: <codeSnippet langauge="C#"> public MyClass() : this("Anonymous") { } public MyClass(string name) { this.name = name; } </codeSnippet> Constructing an instance of the class using the parameterless constructor will result in it's name field containing "Anonymous" because the parameterless constructor delegated to the naming constructor. Also note that you can defer to a base class constructor in the same manner: <codeSnippet langauge="C#"> public MySubClass() : base("Hello World!") { } </codeSnippet> Assuming the base class had a constructor which took a string, the string value "Hello World!" would be passed. HTH, Drew .NET MVP You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.