Re: Creating a "public only" constructor

Dean Cleaver <[email protected]> Mon, 6 Apr 2009 22:15:26 +1200
Newsgroups gmane.comp.windows.devel.dotnet.advanced
Message-ID <8E588B65FD82DF448592A3219102C395035CD2EE74@SXSLAKL001.xceptionsoftware.com>
Because I need a default one for the public side. The second constructor is for a specialised purpose, but I require that if a class is constructed with that constructor, then all classes it creates need to be done in the same fashion. The default constructor creates a blank parameter similar to the second one that can be passed to any class, but it's just ensuring that any class created *always* passes it's parameters to any child class regardless of how it was created that is the issue.

-----Original Message-----
From: Discussion of advanced .NET topics. [mailto:[email protected]] On Behalf Of Simon Robinson
Sent: Monday, 06 April 2009 21:48
To: [email protected]
Subject: Re: [ADVANCED-DOTNET] Creating a "public only" constructor

You can't make a constructor inaccessible within the class but accessible outside it. 

In theory, you could have the default constructor look at the stack trace, and throw an exception if the stack trace shows it's been called from somewhere internal that you don't want to call the constructor, but I wouldn't exactly regard that as a well-architected solution :)

But I'm puzzled - if you don't want code to access the default constructor, why have one at all? You said that using the default constructor is likely to 'make a mess of the system' but in later posts you also say that existing code already uses it. How come that code doesn't make a mess of the system? :)

If you wanted to remove the default constructor, it probably wouldn't be hard to do: Just comment it out without making any other changes, and the compilation errors will immediately tell you which other lines of code you need to refactor.

Simon


On Mon, 6 Apr 2009 16:02:43 +1200, Dean Cleaver <[email protected]> wrote:

>The second constructor allows you to create a business services object on a different connection to the default - so I can specifically create business services objects connected to 2 different servers from within the same application. I then want to ensure that if I create an Invoice object on a given server, than any InvoiceItems it creates are on the same server, which would be done by using the second constructor. If a developer accidentally uses the default constructor, it could make a mess of the system.
>
>-----Original Message-----
>From: Discussion of advanced .NET topics. [mailto:[email protected]] On Behalf Of Shawn Wildermuth
>Sent: Monday, 06 April 2009 15:55
>To: [email protected]
>Subject: Re: [ADVANCED-DOTNET] Creating a "public only" constructor
>
>I don't think you can.  Can you explain why you want to protect it from
>internal classes?
>
>Thanks,
>
>Shawn Wildermuth
>http://wildermuth.com
>https://agilitrain.com
>Microsoft MVP (C#), MCSD.NET, Author and Speaker
>
>The Silverlight Tour is coming to a city near you! 
>
>
>-----Original Message-----
>From: Discussion of advanced .NET topics.
>[mailto:[email protected]] On Behalf Of Dean Cleaver
>Sent: Sunday, April 05, 2009 11:42 PM
>To: [email protected]
>Subject: [ADVANCED-DOTNET] Creating a "public only" constructor
>
>Hey, anyone know if you can do this with attributes or something? I want to
>have 2 public constructors:
>
>public ClassName()
>{
>}
>
>public ClassName(some param)
>{
>}
>
>I want both publically accessible, but I want the default one *inaccessible*
>internally. Can that be done?
>
>Dino
>
>===================================
>View archives and manage your subscription(s) at
>http://peach.ease.lsoft.com/archives
>
>===================================
>View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives
>
>===================================
>View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives

===================================
View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives

===================================
View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives