473,549 Members | 2,346 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1339
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******** *************@h 48g2000cwc.goog legroups.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.Method 1(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.govwro te in message
news:OB******** ******@TK2MSFTN GP03.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******** *************@h 48g2000cwc.goog legroups.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******** *************@h 48g2000cwc.goog legroups.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.Instanc e.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******** *************@p 79g2000cwp.goog legroups.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.Method 1(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
3760
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
2740
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 if it would be easy enough to create a code skeleton out of unit test module. Consider the following, though contrived, unit test code snippet: ...
15
6727
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 javascript or vbscript it works. I will appreciate any help. I do the following (C#):
17
1923
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
3866
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 case statement like this: sub showHelp(byval frm as String) Select Case (frm) Case "Form1" dim help as new Form1
26
5341
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 because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... ...
6
1280
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. The implementation of GetOrderable is _always_ exactly the same in each class that implements IProviderFileVersionNumber, and always will be. It...
1
1463
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 adding a shortcut. Now we have twor versions of the database, both incomplete. How can they be combined w/o losing or duplicating data?
7
10581
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 because I have written a generic plugin system for my current and future C# needs. I defined a base plugin system interface named IPlugin...
0
7471
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7740
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7985
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5111
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3496
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1962
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
784
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.