[Mono-list] Constructors in C#

Brice Carpentier brice@daknet.org
Fri, 19 Nov 2004 21:17:21 +0100


Tom Shelton wrote:
> You cant do the validity checks in the other constructor?
> 
> public  MyClass 
> {
> 	private string member;
> 
> 	public MyClass () : this ("stuff") {}
> 
> 	public MyClass (string s)
> 	{
> 		if (ValidityCheck (s))
> 		{
> 			this.member = s;
> 		}
> 		else
> 		{
> 			doWhatYouWantToDo();
> 		}
> 	}
> }
> 
> I'm assuming that you need to do the same checks when MyClass (string) is
> called directly of course :)
> 		
> Tom Shelton
well, no, they've got no meaning in the other one
basically it's something like that :

public class MyClass
{
	private string login;
	private int uid;
	private string home;

	public MyClass (string line)
	{
		System.text.RegularExpression.Regex.Split(line, ":");
		/* make some checks
		 * (second field is an int, and so on ...) */
		// call next constructor
	}

	public MyClass (string login, int uid, string home)
	{
		// affect things
	}
}

-- 
Brice Carpentier aka Br|ce