Solved

Create a non-instancable class

Posted on 2004-04-23
14
573 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now