
new Singleton() ( Can I override the new operator to return a singleton?)
How about:
public class foo
{
private static foo retFoo = null;
private foo() {}
public static foo Instance()
{
if(retFoo == null)
retFoo = new foo();
return retFoo;
}
public void func()
{
....
}
Quote:
}
public class fooBar
{
private foo foo_instance;
public fooBar()
{
foo_instance = foo.Instance();
}
public void func()
{
foo_instance.func();
}
Quote:
}
Quote:
> I know the Singleton-pattern (Both with and without
> a static constructor). My problem is an implemenation
> that works with the new-operator.
> Besides
> >private static foo retFoo;
> Should be (I think)
> private static foo retFoo = null;
> Olav
> ************************************************************
Quote:
> > Try this:
> > public class foo
> > {
> > private static foo retFoo;
> > private foo() {}
> > public static foo Instance()
> > {
> > if(retFoo == null)
> > retFoo = new foo();
> > return retFoo;
> > }
> > }
> > > > Declare the constructor as Shared.
> > > > Shared Sub New()
> > > > '...
> > > > End Sub
> > > > ...Ashok
> > > Perhaps in VB.NET, but in C# it doesn't change anything
> > > (The default non-static constructor is still there and is
> > > called)
> > > Olav
> > > > > Is there some way to have the new-operator
> > > > > return a Singleton?