Avatar of AlHal2
AlHal2
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Singleton example 2

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

C#Microsoft Visual Studio

Avatar of undefined
Last Comment
AndyAinscow

8/22/2022 - Mon
AndyAinscow

>>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();
AlHal2

ASKER
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?
AndyAinscow

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;
}
}
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
AndyAinscow

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.
AndyAinscow

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.
AlHal2

ASKER
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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
AndyAinscow

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
AndyAinscow

AlHal2

ASKER
Thanks.  I'll try and find some examples online and come back to you with any further questions.
AndyAinscow

Read up about the static keyword, things should become a lot clearer when you understand what that is doing.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23