?
Solved

Create a non-instancable class

Posted on 2004-04-23
14
Medium Priority
?
590 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

649 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