PC Review


Reply
Thread Tools Rate Thread

Creating the same class w/o duplicating?

 
 
Brett Romero
Guest
Posts: n/a
 
      7th Sep 2006
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

 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      7th Sep 2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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
>



 
Reply With Quote
 
Brett Romero
Guest
Posts: n/a
 
      7th Sep 2006
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

 
Reply With Quote
 
Brett Romero
Guest
Posts: n/a
 
      7th Sep 2006

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

 
Reply With Quote
 
Simon Tamman
Guest
Posts: n/a
 
      7th Sep 2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >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
> >

>
>



 
Reply With Quote
 
SP
Guest
Posts: n/a
 
      7th Sep 2006

"Brett Romero" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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

 
Reply With Quote
 
Brett Romero
Guest
Posts: n/a
 
      8th Sep 2006
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

 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      8th Sep 2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
creating a simple class, a cat class? Ron Microsoft VB .NET 5 13th Apr 2007 03:19 AM
Not creating duplicating worksheets with the same name ReD-DevIL Microsoft Excel Programming 7 23rd Mar 2006 02:13 PM
keep from duplicating numbers when creating a list JoelAM Microsoft Excel Worksheet Functions 1 10th Jun 2004 12:54 AM
re: Creating COM out of a .NET class sampath Microsoft Dot NET Framework 0 27th Feb 2004 09:01 AM
Tasks duplicating/Contacts duplicating Kathryn Ingram Microsoft Outlook 0 8th Aug 2003 04:10 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:34 AM.