0

Just recently started to try and learn some designs patterns. Currently trying to get my singleton to return a new object. However it keeps throwing the error "Cannot convert method group 'getInstance' to non-delegate type 'MainWdinow.CustomerLoader'. did you intend to invoke the method?

here is the code for the design pattern method

  public class CustomerLoader
    {
        private static Customer firstInstance = null;
        public static Customer getInstance()
        {
            if(firstInstance ==null)
            {
                firstInstance = new Customer();
            }

            return firstInstance;
        }
    }

Here is where I try to call the method and I get the error mentioned above

CustomerLoader t = CustomerLoader.getInstance();

I want my singleton to do the job of the code below and create a new instance of the customer object

Customer T = new Customer;
4
  • 2
    First look and I think you should call it like this Train t = CustomerLoader.getInstance(); Also are you trying to create a Train or Customer? Any inheritance there? Maybe you want to have private static Customer firstIntance = null; and then you can do Customer t = CustomerLoader.getInstance(); Commented Dec 7, 2018 at 10:47
  • @BART thank you for the fix and helping the newb out! :) Commented Dec 7, 2018 at 10:51
  • Also look at Lazy<T>... it's meant for what you're doing there. Commented Dec 7, 2018 at 10:53
  • @BART yep was meant to be customer throughout instead of train, not had a coffee yet, sorry for any confusion, made an edit and updated the code! thanks again Commented Dec 7, 2018 at 10:54

3 Answers 3

2

Use this. It's also thread safe unlike your version

private static readonly Lazy<Customer> _instance = new Lazy<Customer>(() => new Customer());
public static Customer Instance => _instance.Value;

But you should really use dependency injection instead singletons.

And your naming is off, it looks like Java, check this https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-type-members

private members are not covered by guidelines. But even Microsoft uses _camelCase for private fields in corefx https://github.com/dotnet/corefx/blob/master/Documentation/coding-guidelines/coding-style.md

Sign up to request clarification or add additional context in comments.

10 Comments

This is how I do my singleton classes, if anyone has a better way I would appreciate you posting it. I actually do public static Session Instance { get { return lazy.Value; } } to return the object, I presume => does the same?
BTW, thread-safe yes, but only if you use one of the other constructors that specify you want it to be.
@sellotape what does that has to with property being thread safe?
@Matt yes => lazy.Value; is same as { get { return lazy.Value; } } learn.microsoft.com/en-us/dotnet/csharp/programming-guide/…
@mjwills true about OP's original code, that it will do retrying if there is an exception.
|
0

Use this example:

public class Singleton    
{
    private static Singleton instance = null;
    private static readonly object padlock = new object();

Singleton()
{
}

public static Singleton Instance
{
    get
    {
        lock (padlock)
        {
            if (instance == null)
            {
                instance = new Singleton();
            }
            return instance;
        }
    }
}
}
  1. Singleton should be about one class, not three.
  2. Be careful with it cause your implementation is not thread-safe. See this padlock variable here. It prevents creating multiple instances in a multi-treaded applicatoins.

Comments

-1
CustomerLoader.getInstance

Is a function, you cant assign it to CustomerLoader

Your code should look more like that:

class Train
{
    protected Train() { }

    protected static Train instance;

    public static Train GetSingeltonInstance()
    {
        if(instance == null)
        {
            instance = new Train();
        }

        return instance;
    }
}

class TainUser
{
    private readonly Train train;
    public TainUser()
    {
        train = Train.GetSingeltonInstance();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.