-1

My Singleton class have a constructor like this :

private LanDataEchangeWCF_Wrapper(
 // ILanDataEchangeWCFCallback callbackReciever ,// No error
    ILanDataEchangeWCFCallback callbackReciever = new LanCallBackDefaultHandler(), // Error
    bool cleanExistingConnection = true,
    bool requirePingToKeepAlive = true,
    int pingFrequency = 30000)
{
    if (cleanExistingConnection)
    {
        ExistingConnectionCleaner();
    }
    InitWs(callbackReciever);

    if (requirePingToKeepAlive)
    {
        timer = new Timer(pingFrequency);
        timer.Enabled = true;
        timer.Elapsed += KeepAlive;
    }
}

With LanCallBackDefaultHandler an implementation of the interface ILanDataEchangeWCFCallback

class LanCallBackDefaultHandler : ILanDataEchangeWCFCallback
{
    public void WakeUP(int newID, string entity)
    {
        throw new NotImplementedException();
    }
}

I want to be able to call LanDataEchangeWCF_Wrapper() without implementing the following overload that will be 99% code duplication:

private LanDataEchangeWCF_Wrapper(
    bool cleanExistingConnection = true,
    bool requirePingToKeepAlive = true,
    int pingFrequency = 30000)
{
    if (cleanExistingConnection)
    {
        ExistingConnectionCleaner();
    }
    InitWs(new LanCallBackDefaultHandler());

    if (requirePingToKeepAlive)
    {
        timer = new Timer(pingFrequency);
        timer.Enabled = true;
        timer.Elapsed += KeepAlive;
    }
}

When trying to figure out how to do it the last error I had was

Default parameter need to be compile time constante

With constructor I can't do something like a simple function overload, that will remove code duplication:

private object Methode() 
{
    return new Methode(new LanCallBackDefaultHandler());
}
private object Methode(ILanDataEchangeWCFCallback callbackReciever){
    //Do things
    return ;
}

How can I obtain a compile time constant new instance of the object ?

Drag and Drop
  • 2,672
  • 3
  • 25
  • 37
  • You can't, but you can make the default 'null' and use a private static instance if null is provided. – Silvermind Jun 12 '18 at 14:23
  • Like `callbackReciever = callbackReciever ?? new LanCallBackDefaultHandler();`. Damn you were fast to give me just the little push I needed. – Drag and Drop Jun 12 '18 at 14:24
  • So fast that it must be a common question or something trivial. Lets the quest for dupe began. – Drag and Drop Jun 12 '18 at 14:26
  • Small remark: you actually can overload constructors: `LanDataEchangeWCF_Wrapper() : this(new LanCallBackDefaultHandler());` – Lennart Stoop Jun 12 '18 at 14:27
  • @LennartStoop, Ok I need coffee or a break. Yes it's pretty simple when you use constructor overload. Do you mind adding the parameter and make it an answer ? – Drag and Drop Jun 12 '18 at 14:29

2 Answers2

1

What I usually do is assign a default value of null then check if it's null and assign it to a new object. Similar to below.

private LanDataEchangeWCF_Wrapper(
    ILanDataEchangeWCFCallback callbackReciever = null,
    bool cleanExistingConnection = true,
    bool requirePingToKeepAlive = true,
    int pingFrequency = 30000)
{
    callbackReciever = callbackReciever ?? new LanCallBackDefaultHandler();


    //Rest of constructor
}
kjr1995
  • 420
  • 1
  • 5
  • 9
1

Constructor overload ?

private LanDataEchangeWCF_Wrapper(bool cleanExistingConnection = true, 
                                  bool requirePingToKeepAlive = true, 
                                  int pingFrequency = 30000)
: this (new LanCallBackDefaultHandler(), 
        cleanExistingConnection, 
        requirePingToKeepAlive, 
        pingFrequency) {}

private LanDataEchangeWCF_Wrapper(ILanDataEchangeWCFCallback callbackReciever,
                                  bool cleanExistingConnection = true,
                                  bool requirePingToKeepAlive = true,
                                  int pingFrequency = 30000)
{
    if (cleanExistingConnection)
    {
        ExistingConnectionCleaner();
    }
    InitWs(callbackReciever);

    if (requirePingToKeepAlive)
    {
        timer = new Timer(pingFrequency);
        timer.Enabled = true;
        timer.Elapsed += KeepAlive;
    }
}
Spotted
  • 4,021
  • 17
  • 33