473,320 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Making A Constructor Only Available To Another Class that Doesn't Inherit From It

My scenario involves two classes and a database. I have the classes
"Broom" and "Closet". I want to use a static method from the "Closet"
class to search the database for a matching "Broom". If it finds a
matching "Broom", i want it to return a "Broom" object to the calling
program. I want the static method to call the constructor for the
"Broom" class. I want this static method, (and one other called
"CreateBroom") to be the only ones that can call the "Broom"
constructor. In other words, I don't want the "Broom" constructor to
be callable from the main program code.

I don't want to have the "Closet" class inherit from the "Broom" class,
(and thereafter make the "Broom" constructor protected) because I don't
want the other methods and properties of the "Broom" class to be
accessible from an instance of the "Closet" class.

I can't put the "Broom" class inside of the "Closet" class, (thereby
making it's constructor available only to the "Closet" methods) because
then a "Broom" object cannot be instantiated on it's own to the calling
program by the static method.

Please, any advice would be appreciated. If I am wrong on any points
or there are any alternatives that I have not considered, don't
hesitate to illuminate me.

Thank you.

Jul 31 '06 #1
5 2088
My scenario involves two classes and a database. I have the classes
"Broom" and "Closet". I want to use a static method from the "Closet"
class to search the database for a matching "Broom". If it finds a
matching "Broom", i want it to return a "Broom" object to the calling
program. I want the static method to call the constructor for the
"Broom" class. I want this static method, (and one other called
"CreateBroom") to be the only ones that can call the "Broom"
constructor. In other words, I don't want the "Broom" constructor to
be callable from the main program code.
then you wrote.
I can't put the "Broom" class inside of the "Closet" class, (thereby
making it's constructor available only to the "Closet" methods) because
then a "Broom" object cannot be instantiated on it's own to the calling
program by the static method.
I am not following you.

"In other words, I don't want the "Broom" constructor to be callable from
the main program code."

"because then a "Broom" object cannot be instantiated on it's own to the
calling program by the static method."

Paint me confused ... perhaps you can explain a bit more what your problem
is with the nested class?

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

<ff******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
My scenario involves two classes and a database. I have the classes
"Broom" and "Closet". I want to use a static method from the "Closet"
class to search the database for a matching "Broom". If it finds a
matching "Broom", i want it to return a "Broom" object to the calling
program. I want the static method to call the constructor for the
"Broom" class. I want this static method, (and one other called
"CreateBroom") to be the only ones that can call the "Broom"
constructor. In other words, I don't want the "Broom" constructor to
be callable from the main program code.

I don't want to have the "Closet" class inherit from the "Broom" class,
(and thereafter make the "Broom" constructor protected) because I don't
want the other methods and properties of the "Broom" class to be
accessible from an instance of the "Closet" class.

I can't put the "Broom" class inside of the "Closet" class, (thereby
making it's constructor available only to the "Closet" methods) because
then a "Broom" object cannot be instantiated on it's own to the calling
program by the static method.

Please, any advice would be appreciated. If I am wrong on any points
or there are any alternatives that I have not considered, don't
hesitate to illuminate me.

Thank you.

Jul 31 '06 #2
If Broom and Closet are in the same assembly, you can make Broom's
constructor internal. That way only those classes in the same assembly can
have access to the constructor. If there are other classes in this assembly,
they will of course have access to it as well, but since you are presumably
writing this assembly to be used by others, this should be sufficient.

<ff******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
My scenario involves two classes and a database. I have the classes
"Broom" and "Closet". I want to use a static method from the "Closet"
class to search the database for a matching "Broom". If it finds a
matching "Broom", i want it to return a "Broom" object to the calling
program. I want the static method to call the constructor for the
"Broom" class. I want this static method, (and one other called
"CreateBroom") to be the only ones that can call the "Broom"
constructor. In other words, I don't want the "Broom" constructor to
be callable from the main program code.

I don't want to have the "Closet" class inherit from the "Broom" class,
(and thereafter make the "Broom" constructor protected) because I don't
want the other methods and properties of the "Broom" class to be
accessible from an instance of the "Closet" class.

I can't put the "Broom" class inside of the "Closet" class, (thereby
making it's constructor available only to the "Closet" methods) because
then a "Broom" object cannot be instantiated on it's own to the calling
program by the static method.

Please, any advice would be appreciated. If I am wrong on any points
or there are any alternatives that I have not considered, don't
hesitate to illuminate me.

Thank you.

Jul 31 '06 #3
I apologize. I left some parts out. I will be calling the static
method from an ASP.NET web page, (which is actually a third class - in
the same assembly, I believe). I first tried and failed with the
following code:

************************************************** ************************************
public class Closet
{
public static Broom FindBroom(string BroomName)
{
//Code to search the database for the Broom and return a
//table with the details of the Broom

Broom sweepsAlot = Broom(--Details--);

return sweepsAlot;
}

class Broom
{
//Declare properties with get and set accessors

public Broom(--Details)
{
//Set properties to details
}
}
}

public partial class WebPage : System.Web.UI.Page
{
//Stuff

Broom woodenHandle = Closet.FindBroom(BroomName);
}
************************************************** **************************

With that code, the "Broom" object type wasn't available from WebPage.

If I make "Broom" a class file by itself, someone will be able to call:

Broom nylonBristles = new Broom();

from WebPage. I can't allow that.

I hope that helps the situation make sense. Thank you for your quick
replies. More help would be much appreciated.

Jul 31 '06 #4
You can just make the constructor for the broom private and use it as a
nested class ...

example:

public class Closet {
public class Broom {
public readonly string Name;
private Broom(string _Name) {
Closet = _Closet;
Name = _Name;
}
}

public static Broom CreateBroom(string _Name) {
return new Broom(_Name);
}
}
class Program {
static void Main(string[] args) {
Closet.Broom b = Closet.CreateBroom("Test");
}
}

even better would be to not expose the broom class at all but have an
ISweeper interface that was public .. the closet then could return the broom
as an ISweeper and the broom class would be completely hidden.
Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

<ff******@gmail.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
>I apologize. I left some parts out. I will be calling the static
method from an ASP.NET web page, (which is actually a third class - in
the same assembly, I believe). I first tried and failed with the
following code:

************************************************** ************************************
public class Closet
{
public static Broom FindBroom(string BroomName)
{
//Code to search the database for the Broom and return a
//table with the details of the Broom

Broom sweepsAlot = Broom(--Details--);

return sweepsAlot;
}

class Broom
{
//Declare properties with get and set accessors

public Broom(--Details)
{
//Set properties to details
}
}
}

public partial class WebPage : System.Web.UI.Page
{
//Stuff

Broom woodenHandle = Closet.FindBroom(BroomName);
}
************************************************** **************************

With that code, the "Broom" object type wasn't available from WebPage.

If I make "Broom" a class file by itself, someone will be able to call:

Broom nylonBristles = new Broom();

from WebPage. I can't allow that.

I hope that helps the situation make sense. Thank you for your quick
replies. More help would be much appreciated.

Jul 31 '06 #5
Excellent idea. I'll instantiate it as a Closet.Broom

You have been most helpful. Thank you.

Jul 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
20
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
5
by: Andy B | last post by:
I am trying to figure out how to make an object instance available for all methods of a class. I tried to do something like this: public class test { TheObject Instance = new TheObject();...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
34
by: =?ISO-8859-1?Q?Marcel_M=FCller?= | last post by:
Hi, is there a way to avoid the automatic copy constructor generation. I do not want the object to be non-copyable. I simply do not want that copying is done by the default copy constructor. But...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.