T
Tom
This is not intuitivelly clear.
Kristofer Gafvert said:Use a static method when the method does not belong to a specific object.
Basically is all boils down to memory usage. a static method is shared,
where as a non-static method gets created for every instance of the class.
Hope this makes sense?
John Vottero said:[snip]
Basically is all boils down to memory usage. a static method is shared,
where as a non-static method gets created for every instance of the class.
Hope this makes sense?
This is not correct. The code is always shared whether it's static or not.
You're thinking of data, there's only one instance of static data,
non-static data is created for every instance.
John Vottero
Tom said:What if the class is a singleton?
For instance, I have a WinForm application that does not allow multiple
instances of the application to run on the same workstation. This
application uses a Business Object singleton that caches global variables,
such as a reference to a DataSet that can be used by multiple MDI child
forms.
It doesn't seem particularly useful or efficient to create a new instance
every time I want to use one of the methods in this singleton.
AlexS said:Good point,
as of today it is not possible to declare static method as virtual or
override. I am not very deep in OO internals, but I wonder why it is so.
Why static method can't be virtual or overriden in derived class? Maybe it
has not much sense, however doesn't seem also too much against OO
principles. It could be fun to be able to override static methods in system
classes - seems like ideal system hook functionality.
Another point is impossibility to declare static interface method. I seem
not to grasp yet if and when it could be useful. Anybody can throw in some
ideas?
Jon Skeet said:If your code even *allows* you to create a new instance each time, it's
no longer a singleton, by definition.
Tom said:Jon Skeet said:If your code even *allows* you to create a new instance each time, it's
no longer a singleton, by definition.
No. I am using the following format to create my singleton:
namespace Company.TWS.BusinessObject
{
public class SummaryBO
{
private static TwsDS _twsDS;
private static string xmlLocation = @"c:\data\tws\tws.xml";
private SummaryBO() {}
public static readonly SummaryBO Instance = new SummaryBO();
public static TwsDS twsDS { get { return _twsDS; } }
[...]
}
}
Jon Skeet said:I'm not sure why you've got an instance at all, if everything's static.
The normal singleton pattern would have the _twsDS and twsDS
field/property as an instance, and you'd refer to it using
SummaryBO.Instance.TwsDS
That *doesn't* create an instance each time.
Tom said:OK. I think I understand.
It sounds like what you are saying is create all methods and fields as
normal instance fields within the singlton rather than static (as I was
doing), and the mere fact that it is a singleton will give the smae effect
as acheived by creating all static.
Is that correct? (If so, it does seem much cleaner)