473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Creating the same class w/o duplicating?

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
7 1333
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
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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ovid | last post by:
Hi, I'm trying to determine the cleanest way to override class data in a subclass. class Universe { public String name; private static double PI = 3.1415; Universe(String name) {
4
by: Edvard Majakari | last post by:
Greetings, fellow Pythonistas! I'm about to create three modules. As an avid TDD fan I'd like to create typical 'use-cases' for each of these modules. One of them is rather large, and I wondered...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
17
by: Lee Harr | last post by:
I understand how to create a property like this: class RC(object): def _set_pwm(self, v): self._pwm01 = v % 256 def _get_pwm(self): return self._pwm01 pwm01 = property(_get_pwm, _set_pwm)
9
by: MikeB | last post by:
Hi, I'd appreciate some help, please. I'm writing a VS2005 VB project for school and one of the requirements is that every screen should have a "Help" button. I could do it by writing a clumsy...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
6
by: StevenECBrown | last post by:
I'm somewhat of a newbie. This question has come up for me a couple of times, and it came up again today: I have an interface IProviderFileVersionNumber that has this method called GetOrderable....
1
by: mmurrayepl | last post by:
How can I combine 2 versions of the same database without losing or duplicating data? We have a periodicals checkin database at the Library. A user copied the database to the desktop instead of...
7
by: WTH | last post by:
I am now aware (I am primarily a C++ developer) that in C# if you reference the same interface from the same file in two different projects the types are actually incompatible. I found this out...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.