Create a non-instancable class

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...
LVL 11
SlimshaneeyAsked:
Who is Participating?
 
rdrunnerConnect With a Mentor Commented:
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
 
AlexFMCommented:
Make class constructor private.
0
 
SlimshaneeyAuthor Commented:
It says that
Types declared 'Private' must be inside another type
when i call the class:
Private Class functions
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
rdrunnerCommented:
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
 
Mikal613Commented:
why dont you just make a module
0
 
SlimshaneeyAuthor Commented:
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
 
SlimshaneeyAuthor Commented:
Its a bit of alearning thing more than anyhting.
0
 
MajinLokiCommented:
You should look up the singleton pattern.  I believe this pattern will best suit your needs.
0
 
SlimshaneeyAuthor Commented:
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
 
MajinLokiCommented:
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
 
SlimshaneeyAuthor Commented:
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
 
rdrunnerCommented:
Yes...
0
 
SlimshaneeyAuthor Commented:
Nice one, thanks.
0
 
rdrunnerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.