By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,524 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,524 IT Pros & Developers. It's quick & easy.

Restricting access to Public Sub New()

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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*****@featherbrain.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 #2

P: n/a
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*****@featherbrain.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 #3

P: n/a
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 MyRestrictedClass =
someObject.MethodWhichReturnsRestrictedAnInstanceO fMyRestrictedClass()

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

Dim mrc As MyRestrictedClass = 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.dk> 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*****@featherbrain.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

P: n/a
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*****@emailkey.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*****@featherbrain.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

P: n/a
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*****@featherbrain.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*****@emailkey.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*****@featherbrain.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

P: n/a
D#mn, that works!!!

Much thanks, David.

Dan
On Fri, 30 Jul 2004 05:31:35 -0700, "David Williams , VB.NET MVP"
<da********************@earthlink.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*****@featherbrain.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*****@emailkey.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*****@featherbrain.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 discussion thread is closed

Replies have been disabled for this discussion.