469,578 Members | 1,869 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,578 developers. It's quick & easy.

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 1903
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jun | last post: by
20 posts views Thread by Ole Hanson | last post: by
45 posts views Thread by Ben Blank | last post: by
9 posts views Thread by Morten Lemvigh | last post: by
34 posts views Thread by =?ISO-8859-1?Q?Marcel_M=FCller?= | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.