Solved

Create a non-instancable class

Posted on 2004-04-23
14
578 Views
Last Modified: 2008-02-01
Hi there, what is the code to stop instances of a class being created?
I ahve a class that has only shared functions in it and I dont want anyone creating instances of this class...
0
Comment
Question by:Slimshaneey
  • 6
  • 4
  • 2
  • +2
14 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10898109
Make class constructor private.
0
 
LVL 11

Author Comment

by:Slimshaneey
ID: 10898127
It says that
Types declared 'Private' must be inside another type
when i call the class:
Private Class functions
0
 
LVL 11

Expert Comment

by:rdrunner
ID: 10898262
You should delcare the class as MustInherit

This makes it impossible to create an instance of this class at all...

I think thats the correct setting for your class

0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 48

Expert Comment

by:Mikal613
ID: 10899032
why dont you just make a module
0
 
LVL 11

Author Comment

by:Slimshaneey
ID: 10899092
Because I need to have a class that I can call with functions from various functions used in about 12 other dll's, but I dont need it to be instancable, just callable, because of the hidden helper functions inside.
0
 
LVL 11

Author Comment

by:Slimshaneey
ID: 10899096
Its a bit of alearning thing more than anyhting.
0
 
LVL 4

Expert Comment

by:MajinLoki
ID: 10899644
You should look up the singleton pattern.  I believe this pattern will best suit your needs.
0
 
LVL 11

Author Comment

by:Slimshaneey
ID: 10899806
The singleton only allows 1 instance of an object no matter how many times its called. Im looking to use this class only as a shared class, non-instancable.
0
 
LVL 4

Expert Comment

by:MajinLoki
ID: 10899867
The way a class works, you cannot use it, if you cannot create it.  What you want is a code module.

It is my understanding that even if the methods are public, an object must exist for those methods to be called.  Just because I write the code for ClassA and it exists in my project, does not mean that it can be used.  I must create an instance of this class, an object that has its methods and attributes.  If I do not, the Class may as well not exist in my project.

I could be wrong, and I'd love to see how you figure it out.

Majin Loki
0
 
LVL 11

Accepted Solution

by:
rdrunner earned 250 total points
ID: 10899929
Just make it "mustInherit" like i suggested and let the other clases you use also inherit the helper class...

This way they can call the helper functions... since they are part of the new class now... Do i make sense? :)


0
 
LVL 11

Author Comment

by:Slimshaneey
ID: 10899955
If I use the must inherit, I assume then I dont need to use the Shared keyword in my methods to make them available to other classes if I call the link
Public Class MyClass Inherits SharedClass
0
 
LVL 11

Expert Comment

by:rdrunner
ID: 10900073
Yes...
0
 
LVL 11

Author Comment

by:Slimshaneey
ID: 10900112
Nice one, thanks.
0
 
LVL 11

Expert Comment

by:rdrunner
ID: 10900187
I would only inherit the helper class in my base class... So you dont have multiple copies of it if you dont need them

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question