new Singleton() ( Can I override the new operator to return a singleton?) 
Author Message
 new Singleton() ( Can I override the new operator to return a singleton?)

Is there some way to have the new-operator
return a Singleton?

I.e. that
new Singleton()
always return a reference to the same object?

The reason I need this is that I want to share
a DataSet between forms, the dataset must  be
available at design time, and the Windows
Forms Designer seems to want a new-operator.

Thanks!
Olav



Wed, 04 Aug 2004 02:41:45 GMT  
 new Singleton() ( Can I override the new operator to return a singleton?)

Quote:
> Is there some way to have the new-operator
> return a Singleton?

> I.e. that
> new Singleton()
> always return a reference to the same object?

> The reason I need this is that I want to share
> a DataSet between forms, the dataset must  be
> available at design time, and the Windows
> Forms Designer seems to want a new-operator.

untested VB syntax:

Private Shared _Singleton as New MyClass

private Sub New
   'empty. private declaration avoids external creation
end sub

Public Shared Readonly Property Singleton As MyClass
    Get
        Return _Singleton
    End Get
end sub

Accessing MyClass.Singleton always returns the same object.

Armin



Wed, 04 Aug 2004 05:28:50 GMT  
 new Singleton() ( Can I override the new operator to return a singleton?)
Declare the constructor as Shared.
Shared Sub New()
    '...
End Sub

...Ashok


Quote:
> Is there some way to have the new-operator
> return a Singleton?

> I.e. that
> new Singleton()
> always return a reference to the same object?

> The reason I need this is that I want to share
> a DataSet between forms, the dataset must  be
> available at design time, and the Windows
> Forms Designer seems to want a new-operator.

> Thanks!
> Olav



Wed, 04 Aug 2004 11:10:42 GMT  
 new Singleton() ( Can I override the new operator to return a singleton?)
I dont know VB/VB.NET, but I think this is "just"
a standard Singleton-pattern (It might have solved
my problems if I were using VB though).

Olav

Quote:

> untested VB syntax:

> Private Shared _Singleton as New MyClass

> private Sub New
>    'empty. private declaration avoids external creation
> end sub

> Public Shared Readonly Property Singleton As MyClass
>     Get
>         Return _Singleton
>     End Get
> end sub

> Accessing MyClass.Singleton always returns the same object.

> Armin



Wed, 04 Aug 2004 19:26:21 GMT  
 new Singleton() ( Can I override the new operator to return a singleton?)
Quote:

> 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

Quote:



> > Is there some way to have the new-operator
> > return a Singleton?



Wed, 04 Aug 2004 20:37:10 GMT  
 new Singleton() ( Can I override the new operator to return a singleton?)
Try this:
public class foo
{
    private static foo retFoo;
    private foo() {}
    public static foo Instance()
    {
        if(retFoo == null)
            retFoo = new foo();
        return retFoo;
    }
Quote:
}




Quote:
> > 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?



Thu, 05 Aug 2004 02:12:49 GMT  
 new Singleton() ( Can I override the new operator to return a singleton?)
I know the Singleton-pattern (Both with and without
a static constructor).  My problem is an implemenation
that works with the new-operator.

Besides

Quote:
>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?



Thu, 05 Aug 2004 19:38:34 GMT  
 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?



Fri, 06 Aug 2004 22:48:34 GMT  
 new Singleton() ( Can I override the new operator to return a singleton?)

Quote:

>Try this:
>public class foo
>{
>    private static foo retFoo;
>    private foo() {}
>    public static foo Instance()
>    {
>        if(retFoo == null)
>            retFoo = new foo();
>        return retFoo;
>    }
>}

You can use a static constructor to get rid of the test for null:

  class Singleton
  {
    static Singleton instance;
    static Singleton() { instance = new Singleton(); }
    static public Singleton Instance() { return instance; }
    private Singleton() {}
  }

The static constructor is guaranteed to be called first.

--



Fri, 06 Aug 2004 23:46:19 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Global operator new (::new) overriding

2. Singleton not a singleton?

3. ATL Singleton and Singleton Class also from C++

4. How To Override The New / delete Operator ?

5. ? How to override new operator in VC6.0-help

6. overriding operators new and delete

7. overriding operator new[] - (VC 4.2)

8. Overriding the "new" operator

9. Overriding the new operator in debug mode...

10. overriding the new operator gives syntax errors?

11. How To Override The New / delete Operator ?

12. overriding the global new and delete operators

 

 
Powered by phpBB® Forum Software