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 ?