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

F

ffrugone

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.
 
G

Greg Young

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
 
M

Marina Levit [MVP]

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.
 
F

ffrugone

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.
 
G

Greg Young

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
 
F

ffrugone

Excellent idea. I'll instantiate it as a Closet.Broom

You have been most helpful. Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top