Without going into details (as it's all a bit app-specific) I have "normal"
classes for each of the objects my app works with. They are normal so I can
create any number of instances of them, and they have their own methods that
work with the data within.
I also have a static class that holds a lot of the logic for the program
that is independant of the objects and their instances... one example is a
method to write information out to a log file.
From within my instanced object methods, I can simply use:
clsMain.WriteLo g(Message);
instead of
clsMain MainClassInstan ce = new clsMain();
MainClassInstan ce.WriteLog(Mes sage);
so I save a line of code each time... there is more to it than that of
course, and the MS Site gives a good readup.
Hi chandu,
Static methods and non-static methods are really meant for different
things. Use static methods when you don't need an instance of the
class, or access to members of an instance. "Static" implies
class-level scope, not instance-level scope.
Also, it's worth remembering that you cannot make static methods
virtual or abstract (and therefore they cannot be overridden).
John
chandu wrote:
can any one give advantages and disadvantages to using static methods in a
class.
>
shall we use complete static methods in a class ?
>
thanks
My Idvice would be use static methods only of they don't need to access any
class (static) fields. Good example is the System.Math class. As soon as you
fill the need to preserve some internal state (class fields) that is a sign
that this method shouldn't be static. However this is just a guidline there
a many cases that this doesn't apply. Good example for an excption is the
singleton design pattern.
--
HTH
Stoitcho Goutsev (100)
"chandu" <nareshv_chandr a@hotmail.comwr ote in message
news:%23Eqsy3d8 GHA.3736@TK2MSF TNGP02.phx.gbl. ..
can any one give advantages and disadvantages to using static methods in a
class.
>
shall we use complete static methods in a class ?
>
thanks
>
Comment