473,657 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
"CreateBroo m") 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 2114
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
"CreateBroo m") 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.goog legroups.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
"CreateBroo m") 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.goog legroups.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
"CreateBroo m") 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(strin g 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.P age
{
//Stuff

Broom woodenHandle = Closet.FindBroo m(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(str ing _Name) {
return new Broom(_Name);
}
}
class Program {
static void Main(string[] args) {
Closet.Broom b = Closet.CreateBr oom("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.goog legroups.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(strin g 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.P age
{
//Stuff

Broom woodenHandle = Closet.FindBroo m(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
4556
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
20
4251
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 interface and accessing the datastore MUST pass in a UserToken in the constructor of the object. Is this not possible? Am I forced to add the UserToken as a property on the object instead? /Ole
45
6338
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 parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
15
1898
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 two? (1)
13
3957
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 is much appreciated. JD
9
23746
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 of objects using the given constructor. The objects should all inherit from a base class. It's not possible to pass actual objects, since it's not given on beforehand, how many should be created.
5
1891
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(); TheObject.Dictionary<string, string= new Dictionary<string, string>(); .... } The first line (TheObject instance = new TheObject();) doesn't get
50
4471
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): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
34
3663
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 there is a constructor that accepts the base class. This one should be used for copying. In fact i have a set of classes with a common abstract base. The implementations do not have own data members. They only implement
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8603
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7320
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.