473,797 Members | 2,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Restricting access to Public Sub New()

I have a situation where I need to prevent just anyone from instantiating a
particular class. The class is instantiated via certain methods from other
classes in the solution, but those are the only classes/methods allowed to
do this.

How can I prevent all other classes from creating a new instance of my
'restricted' class? Or, to put it another way, how can the restricted class
'know' who is trying to instantiate it?

Thanks,
Daniel Klein
Dublin, Ohio USA
Nov 20 '05 #1
6 2643
Daniel,

Could you declare your Class as Protected?

See MSDN for Class statement reference:
http://msdn.microsoft.com/library/de...vastmclass.asp

René

"Daniel Klein" <da*****@feathe rbrain.net> wrote in message
news:d7******** *************** *********@4ax.c om...
I have a situation where I need to prevent just anyone from instantiating a particular class. The class is instantiated via certain methods from other
classes in the solution, but those are the only classes/methods allowed to
do this.

How can I prevent all other classes from creating a new instance of my
'restricted' class? Or, to put it another way, how can the restricted class 'know' who is trying to instantiate it?

Thanks,
Daniel Klein
Dublin, Ohio USA

Nov 20 '05 #2
Another option might be to make the constructor private and provide a shared
method called Create() that returns the constructed object or something like
that?
"Daniel Klein" <da*****@feathe rbrain.net> wrote in message
news:d7******** *************** *********@4ax.c om...
I have a situation where I need to prevent just anyone from instantiating a particular class. The class is instantiated via certain methods from other
classes in the solution, but those are the only classes/methods allowed to
do this.

How can I prevent all other classes from creating a new instance of my
'restricted' class? Or, to put it another way, how can the restricted class 'know' who is trying to instantiate it?

Thanks,
Daniel Klein
Dublin, Ohio USA

Nov 20 '05 #3
René

I tried that. Problem is that instantiation of the class is not related to
inheritance.

I want to be able to do:

Dim mrc As MyRestrictedCla ss =
someObject.Meth odWhichReturnsR estrictedAnInst anceOfMyRestric tedClass()

but I don't want outside code to be able to do:

Dim mrc As MyRestrictedCla ss = New MyRestrictClass ()

I'm going to give Richard Tappenden's idea a try.

But thanks for responding anyway :-)

Dan
On Fri, 30 Jul 2004 12:25:39 +0200, "René Nordby" <re**@bluedot.d k> wrote:
Daniel,

Could you declare your Class as Protected?

See MSDN for Class statement reference:
http://msdn.microsoft.com/library/de...vastmclass.asp

René

"Daniel Klein" <da*****@feathe rbrain.net> wrote in message
news:d7******* *************** **********@4ax. com...
I have a situation where I need to prevent just anyone from instantiating

a
particular class. The class is instantiated via certain methods from other
classes in the solution, but those are the only classes/methods allowed to
do this.

How can I prevent all other classes from creating a new instance of my
'restricted' class? Or, to put it another way, how can the restricted

class
'know' who is trying to instantiate it?

Thanks,
Daniel Klein
Dublin, Ohio USA


Nov 20 '05 #4
Ahhh, kinda like what you have to do for a Singleton. However, what's to
prevent outside code from using the Create() method?

Dan

On Fri, 30 Jul 2004 11:31:51 +0100, "Richard Tappenden"
<ri*****@emailk ey.com> wrote:
Another option might be to make the constructor private and provide a shared
method called Create() that returns the constructed object or something like
that?
"Daniel Klein" <da*****@feathe rbrain.net> wrote in message
news:d7******* *************** **********@4ax. com...
I have a situation where I need to prevent just anyone from instantiating

a
particular class. The class is instantiated via certain methods from other
classes in the solution, but those are the only classes/methods allowed to
do this.

How can I prevent all other classes from creating a new instance of my
'restricted' class? Or, to put it another way, how can the restricted

class
'know' who is trying to instantiate it?

Thanks,
Daniel Klein
Dublin, Ohio USA


Nov 20 '05 #5
If the only ones that should be able to create an instance of the class
are in the same assembly, why not mark the class as Friend? This would
make that no one outside the assembly can use it. If you need the class
itself to be available outside the assembly, but only created within the
assembly then you could mark the class Public, but the Sub New as
Friend.

HTH

David

"Daniel Klein" <da*****@feathe rbrain.net> wrote in message
news:pr******** *************** *********@4ax.c om:
Ahhh, kinda like what you have to do for a Singleton. However, what's to
prevent outside code from using the Create() method?

Dan

On Fri, 30 Jul 2004 11:31:51 +0100, "Richard Tappenden"
<ri*****@emailk ey.com> wrote:
Another option might be to make the constructor private and provide a
shared
method called Create() that returns the constructed object or something
like
that?
"Daniel Klein" <da*****@feathe rbrain.net> wrote in message
news:d7******* *************** **********@4ax. com...
I have a situation where I need to prevent just anyone from
instantiating

a
particular class. The class is instantiated via certain methods from
other
classes in the solution, but those are the only classes/methods allowed
to
do this.

How can I prevent all other classes from creating a new instance of
my
'restricted' class? Or, to put it another way, how can the restricted

class
'know' who is trying to instantiate it?

Thanks,
Daniel Klein
Dublin, Ohio USA


Nov 20 '05 #6
D#mn, that works!!!

Much thanks, David.

Dan
On Fri, 30 Jul 2004 05:31:35 -0700, "David Williams , VB.NET MVP"
<da************ ********@earthl ink.net> wrote:
If the only ones that should be able to create an instance of the class
are in the same assembly, why not mark the class as Friend? This would
make that no one outside the assembly can use it. If you need the class
itself to be available outside the assembly, but only created within the
assembly then you could mark the class Public, but the Sub New as
Friend.

HTH

David

"Daniel Klein" <da*****@feathe rbrain.net> wrote in message
news:pr******* *************** **********@4ax. com:
Ahhh, kinda like what you have to do for a Singleton. However, what's to
prevent outside code from using the Create() method?

Dan

On Fri, 30 Jul 2004 11:31:51 +0100, "Richard Tappenden"
<ri*****@emailk ey.com> wrote:
>Another option might be to make the constructor private and provide a
>shared
>method called Create() that returns the constructed object or something
>like
>that?
>
>
>"Daniel Klein" <da*****@feathe rbrain.net> wrote in message
>news:d7******* *************** **********@4ax. com...
>> I have a situation where I need to prevent just anyone from
>> instantiating
>a
>> particular class. The class is instantiated via certain methods from
>> other
>> classes in the solution, but those are the only classes/methods allowed
>> to
>> do this.
>>
>> How can I prevent all other classes from creating a new instance of
>> my
>> 'restricted' class? Or, to put it another way, how can the restricted
>class
>> 'know' who is trying to instantiate it?
>>
>> Thanks,
>> Daniel Klein
>> Dublin, Ohio USA
>


Nov 20 '05 #7

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

Similar topics

2
2712
by: Xenophobe | last post by:
I have a popup window (required by the client) containing a form and would like to prevent users from accessing it directly. They are instead required to access the page via a hyperlink on another page. HTTP_REFERER, while not completely reliable, would serve the purpose except for another problem. The hyperlink points to a JavaScript function which opens the popup. This yields HTTP_REFERER worthless. My other thought was to create a...
7
2045
by: wrytat | last post by:
Hi! I'm very new to ASP.NET and really need some good advice from experts here. I'm creating a web application for my company now. This application has 2 parts. 1 part for the customers to access. The 2nd part is for our staff to access only. My director hopes to make the 2nd part to be something like an intranet, such that only our company's computers (maybe only 1 or 2 in the company) can login to this part of the application. 1. My...
1
1742
by: wrytat | last post by:
Hi! I'm very new to ASP.NET and really need some good advice from experts here. I'm creating a web application for my company now. This application has 2 parts. 1 part for the customers to access. The 2nd part is for our staff to access only. My director hopes to make the 2nd part to be something like an intranet, such that only our company's computers (maybe only 1 or 2 in the company) can login to this part of the application. 1. My...
3
2251
by: MoCha | last post by:
hello everyone, i want to know of any trick used to restrict the access scope of a friend class (or function). ie instead of the friend class having access to the entire internals of the class in question, let it have access to a few private/protected members. this problem is cropping up several times. giving the friend class full access does not seem right n at the same time the function(s) r not reqd by anyone else.
4
4644
by: Dennis C. Drumm | last post by:
Is there a way with C# to allow one class access to a method or field of another class, without making that method or field visible to all other classes, as would be the case when making the method or field public? Thanks, Dennis
0
1005
by: WebMatrix | last post by:
Hello, What's the best way to keep email templates as html files on the server, so ASP.NET application can get file access to it, while restricting web users from accessing it through their browsers. The site is open to the public, no authentication is required, and web application runs under default iis user account. Thanks!
2
1881
by: WittyGuy | last post by:
Hi My class looks something like this: class Base { public: Base () {} ~Base () {} // No virtual dtor in the base class private: };
4
1613
by: Christopher | last post by:
I am surprised this hasn't come up for me more in the past, but the situation is: I need to have an interface that is usable for all I need to have an interface that is only usable for some I do not really know of a good way to achieve this. If I use friend functions, I can no longer make methods virtual, right? Example:
7
4050
by: shashi shekhar singh | last post by:
Respected Sir, I am really tired in solving of this issue that have been arises when i would like to restrict files to access only on my Test page , here i am retriving my files in iframe in Test page, problem occurs when a user authenticated themselves then they will be redirected on welcome page and he can access my files through welcome page on Browser by knowing my Folder Name. but i do'nt want to give permissions to access on welcome...
0
9685
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
10245
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10205
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
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
9063
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...
1
7559
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6802
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();...
1
4131
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
3
2933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.