Solved

Create a non-instancable class

Posted on 2004-04-23
14
579 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

765 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