By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,479 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

Creating the same class w/o duplicating?

P: n/a
I need a static version of a class that can be referenced anywhere as a
singleton and the same class that can be used as instances. Can this
be done without basically creating the same class twice (one with
static methods and one without)?

Thanks,
Brett

Sep 7 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Why do you need a static class? An instance class can contain static
methods.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"Brett Romero" <ac*****@cygen.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
>I need a static version of a class that can be referenced anywhere as a
singleton and the same class that can be used as instances. Can this
be done without basically creating the same class twice (one with
static methods and one without)?

Thanks,
Brett

Sep 7 '06 #2

P: n/a
I don't want a static class. I want one version of the class I can use
as a singleton and one version I can new up over and over. This is a
collection. The singleton version is shared through out the app by
"many" classes. Items are added and removed by various classes in the
singleton version. The non singleton version is used by "one" class to
do temporary work.

Brett

Sep 7 '06 #3

P: n/a

Kevin Spencer wrote:
Why do you need a static class? An instance class can contain static
methods.
Also, how do you use a static method on an instance? You'll get a
compiler error such as:

Error
Static member 'MyClass.Method1(string var1, out classA var2)' cannot be
accessed with an instance reference; qualify it with a type name
instead

Brett

Sep 7 '06 #4

P: n/a
You could create a static instance of the class within the instance class.

public class Foo
{
private static Foo globalInstance = new Foo();
public static Foo GlobalInstance
{
get{return globalInstance;}
}
}
The global instance will have the same methods as an instance version (as
it's still an instance, just a static instance)

However it's not a "proper" singleton if you do that. Singletons are
generally defined as objects with only 1 instance EVER and the constructors
are usually private (they return the one instance in the same kind of way).
The Singleton pattern is nicely defined here:
http://www.yoda.arachsys.com/csharp/singleton.html

HTH

Simon

"Kevin Spencer" <uc*@ftc.govwrote in message
news:OB**************@TK2MSFTNGP03.phx.gbl...
Why do you need a static class? An instance class can contain static
methods.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"Brett Romero" <ac*****@cygen.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
I need a static version of a class that can be referenced anywhere as a
singleton and the same class that can be used as instances. Can this
be done without basically creating the same class twice (one with
static methods and one without)?

Thanks,
Brett


Sep 7 '06 #5

P: n/a
SP

"Brett Romero" <ac*****@cygen.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
>I need a static version of a class that can be referenced anywhere as a
singleton and the same class that can be used as instances. Can this
be done without basically creating the same class twice (one with
static methods and one without)?
That's a contradiction in terms. Instead derive your singleton class from
your base class. Then both classes will have the same methods but the
singleton will be the singleton and the base class will be used for the
instances.

SP

Sep 7 '06 #6

P: n/a
In the end, I turned the class into an abstract. Then I created two
classes (A & B) that inherit the abstract. A and B only have
constructors. B's constructor is private since it is the singleton.
It also have a property in B named Instance that returns the static
instance.

Is there a way to get rid of the common singleton syntax:
MyClass.Instance.DoSomething()?

The .Instance part in other words.

Thanks,
Brett

Sep 8 '06 #7

P: n/a
You can use a static method to work with instances. For example, you can
create a static method that takes an instance of an object as a parameter,
or returns an instance of an object. You can't refer to instance members of
a class from static members.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"Brett Romero" <ac*****@cygen.comwrote in message
news:11*********************@p79g2000cwp.googlegro ups.com...
>
Kevin Spencer wrote:
>Why do you need a static class? An instance class can contain static
methods.

Also, how do you use a static method on an instance? You'll get a
compiler error such as:

Error
Static member 'MyClass.Method1(string var1, out classA var2)' cannot be
accessed with an instance reference; qualify it with a type name
instead

Brett

Sep 8 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.