[Mono-list] Constructors in C#
Robert Shade
rshade@dvsconsulting.com
Fri, 19 Nov 2004 18:49:42 -0500
> seems quite weird to me for something even vb is doing almost perfectly !
>
MS makes use of the static From* methods throughout the runtime, so it's
not a completely "out there" pattern.
Also, VB actually doesn't do what you want either. I believe you were
referring to the example:
Public Class Manager
Private nameField As String
Public Sub New()
'default value for nameField is Tony
Me.New("Tony")
End Sub
Public Sub New(ByVal name As String)
nameField = name
System.Console.WriteLine("I am the manager.")
End Sub
End Class
This deceptively looks like the pattern you want, but if you try to do
anything before to the call to "Me.New("Tony")" you get an error saying
that a "Constructor call is valid only as the first statement in an
instance constructor." So, this means the above is really the same as:
public Manager() : this("Tony")
{
}
in c#.
The only other way i can think of is to take the code from the
constructor you want the other to call and put it in a separate method.
Then, you can have both call that method.
I can't think of any other way to eliminate code duplication otherwise.
rob