Solved

Create a non-instancable class

Posted on 2004-04-23
14
581 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

737 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