Groups | Blog | Home
all groups > c# > september 2004 >

c# : Inheritage - or what??


Arne Rasmussen
9/24/2004 10:35:35 PM
Hi there
I have this problem - might seem stupid to you and a cause of poor design
....but nevertheless it's a problem that i'm facing:

I have 1 to X singletons witch have exactly the same code - but they are in
different namespaces as they hold some information witch belongs to this
namespace.
Anyway they are called in the intizialising of my application and thereby
instantiated - and filled with the proper information .
Now the way i do taht is exactly the same way for all my singletons - and
therby repeated code for each and every.
I would like to have a method which takes a singleton and a ...say
filename... and do the work ...but i can't figure out how to do this - i
have tried with an interface - but as we all now it don't like the keyword
static :-) .....somebody know what to do???? - should mention that i'm a
relative newbee to C#

Thanx in advance
Regards
A. Rasmussen

Scott Allen
9/25/2004 9:15:41 AM
Hi Arne:

If the singleton is a 'single instance of an object', you probably
wouldn't have static members on the type. Perhaps you are implementing
a static class, not a singleton?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/singletondespatt.asp

--
Scott
http://www.OdeToCode.com

On Fri, 24 Sep 2004 22:35:35 +0200, "Arne Rasmussen"
[quoted text, click to view]
Arne Rasmussen
9/25/2004 10:23:35 AM
What i have is the following:

using System;

namespace TestAfInstance.TestClass1
{
public class Class1
{
private static Class1 instance;

private string someVariable;

public static Class1 Instance()
{
if (instance == null)
{
instance = new Class1();
}
return instance;
}

public string SomeVariable
{
get{return someVariable;}
set{someVariable = value;}
}
}
}


Now i have several of these class'es but they differ in namespace.
However i found the solution - i have a baseclass implementing the
property SomeVariable wich the singletons inherits i then do the
following to have common code

BaseClass base = <somenamespace>.instance();

and then accesing base.SomeVariable for all the namespaces

Regards
Arne Rasmussen

*** Sent via Developersdex http://www.developersdex.com ***
AddThis Social Bookmark Button