We help IT Professionals succeed at work.

Singleton example 2

AlHal2
AlHal2 asked
on
82 Views
Last Modified: 2019-02-11
How do I call an instance of this class using C# in Visual Studio 2017?  At the moment I have a console App.  In the static main method I have this code, but can't invoke the Inventory class due to its protection level.

static void Main(string[] args)
        {
            Inventory I1 = new Inventory();
        }

Open in new window


I've seen a similar question here, but I want to understand what's happening rather than just copy and paste https://www.experts-exchange.com/questions/21783726/Singleton-example.html
I'd like to see the class invoked successfully once then produce an error message when I try to invoke it a second time.

    private static readonly Lazy<Inventory> instance = new Lazy<Inventory>(() => new Inventory());
    private Inventory()
    { }
    public static Inventory Instance
    {
        get { return instance.Value; }
    }
    public void LookupProduct(string upc)
    {

    }

Open in new window

Comment
Watch Question

AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
>>I'd like to see the class invoked successfully once then produce an error message when I try to invoke it a second time.

That is rather an odd requirement.  Why?  It sort of defeats the purpose of having a singleton.

A Singleton is designed so there is only one instance possible.  The constructor is coded to prevent a user trying to use the new keyword to make two or more instances.
One uses a singleton as follows for example

Inventory inv = Inventory.Instance;

or
Inventory.Instance.SomeFunctionOrMethod();

Author

Commented:
Should this not produce an error as its invoking it twice?

            Inventory inv = Inventory.Instance;
            Inventory inv2 = Inventory.Instance;

Open in new window

Could you show me how to use the new keyword once to make one instance?
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
You don't seem to understand what the static keyword is.

It is only having one instance. inv and inv2 are actually identical.  

>>Could you show me how to use the new keyword once to make one instance?
Something like this is required.  (Just out of my head, may need mods to compile but you should see the idea).

public class OnceOnly
{
private static bool bIsCreated = false;
public OnceOnly()
{
if(bIsCreated)  throw new Exception("Already Created");
bIsCreated = true;
}
}
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
Note with something like that code snippet (for OnceOnly) you can only create one instance ever in the program, even after the one you create is destroyed.  To only allow one instance at any one time is rather more complex with .net and garbage collection.
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
ps.  The code for OnceOnly doesn't really stop a second instance being created, it does give you a pretty strong warning and will only stop you using a second instance under certain circumstances.  It can be got around.  A really robust method is much more complex.

Author

Commented:
Would you have any code samples eg to stop a program invoking more than one instance of a print queue at a time or log file generator.  For example could the code at the bottom of this article be converted to C#
https://dzone.com/articles/singleton-design-pattern-%E2%80%93
Freelance programmer / Consultant
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:

Author

Commented:
Thanks.  I'll try and find some examples online and come back to you with any further questions.
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
Read up about the static keyword, things should become a lot clearer when you understand what that is doing.