Article View: borland.public.csharpbuilder.language.csharp
Article #396Re: inherited / constructors : best code ?
From: Jon Shemitz
Date: Thu, 21 Jul 2005 22:15
Date: Thu, 21 Jul 2005 22:15
53 lines
2351 bytes
2351 bytes
OD wrote:
> public class A
> {
> public A()
> {
> }
>
> public A(int param):this()
> {
> }
> }
>
> public class B : A
> {
> public class B():base() // Problem 1
> {
> }
>
> public class B(int param):this() // Problem 2
> {
> }
> }
>
> Problem 1 : is using ":base()" correct ?
Yes, although it's redundant: "public class B() {}" is exactly
equivalent to "public class B(): base() {}" (down to the "no
parameter-less constructor" msg, if A has no public/protected
parameter-less constructor.)
> Problem 2, the worst : this constructor can call either ":this()" or
> ":base(param)".
> In the first case I lose the init of the parameter (done in A class),
> in the second case I lose local init of class B done in the first
> constructor...
>
> What is the best construction ? Is there some "best practices" or
> coding patterns for such a situation ?
I don't think case 1 or case 2 is always right: I'd always go with the
version that reads the most clearly and/or takes the least custom
code.
In many cases, of course, the "local init" can get pushed to an
ordinary, private method that simply gets called in two different
constructors. (Though you can't set readonly fields from a normal
instance method.)
--
www.midnightbeach.com
Technical Details
Message-ID:
<42E01E73.C32A6218@midnightbeach.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news.moat.net!news-out.newsfeeds.com!propagator3-LAX.newsfeeds.com!news-in.newsfeeds.com!news-in.superfeed.net!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<mn.ad937d57748784b9.18651@0e-naxos.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 1efe35,40a1073c2533bd08 X-Google-Attributes: gid1efe35,public X-Google-Language: ENGLISH,ASCII-7-bit Message-ID: <42E01E73.C32A6218@midnightbeach.com> Date: Thu, 21 Jul 2005 15:15:15 -0700 From: Jon Shemitz <jon@midnightbeach.com> Reply-To: jon@midnightbeach.com Organization: Midnight Beach X-Mailer: Mozilla 4.79 [en] (Windows NT 5.0; U) X-Accept-Language: en-US,en MIME-Version: 1.0 Newsgroups: borland.public.csharpbuilder.language.csharp Subject: Re: inherited / constructors : best code ? References: <mn.ad937d57748784b9.18651@0e-naxos.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 63.249.104.197 X-Trace: newsgroups.borland.com 1121984114 63.249.104.197 (21 Jul 2005 15:15:14 -0700) Lines: 53 Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news.moat.net!news-out.newsfeeds.com!propagator3-LAX.newsfeeds.com!news-in.newsfeeds.com!news-in.superfeed.net!caladan.landsraad.net!newsgroups.borland.com!not-for-mail