473,378 Members | 1,658 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,378 software developers and data experts.

Using Static Methods Adv?

can any one give advantages and disadvantages to using static methods in a
class.

shall we use complete static methods in a class ?

thanks
Oct 17 '06 #1
5 3825

"chandu" <na*************@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP02.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 ?
Read here: http://msdn2.microsoft.com/en-us/library/79b3xss3.aspx

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.WriteLog(Message);

instead of

clsMain MainClassInstance = new clsMain();
MainClassInstance.WriteLog(Message);

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.

James
Oct 17 '06 #2
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 ?
If you want to use complete static methods in a class, you can declare
that class as static too (supported in .Net 2.0).
>
thanks

Oct 17 '06 #3
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
Oct 17 '06 #4
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" <na*************@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP02.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

Oct 17 '06 #5
chandu wrote:
can any one give advantages and disadvantages to using static methods in a
class.
What is the advantage and disadvantage of using a screwdriver instead
of a hammer ?

Depends a lot on what you are trying to do.

Same with static and non static methods.

If you need a static method you should use a static method.

If you need a non static method you should use a non static method.

If you are new to programming then you may often be in doubt about
what you need.

But try and think about whether the call is related to the
class or to the instance.

Arne

Oct 17 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Sparks | last post by:
Anyway... At Europython Guido discussed with everyone the outstanding issue with decorators and there was a clear majority in favour of having them, which was good. From where I was sitting it...
4
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to define static class methods and data members in Python (similar to the way it can be done in C++ or Java). These do not seem to be mentioned...
9
by: Simon Harvey | last post by:
Hi all, In my project I have made a number of helper methods static. As I understand it, this will create the problem that multiple threads could access the static method at the same time and...
0
by: Jim dunn | last post by:
HI I am having problems with C# with regards to its compatibility with win32 API methods, I am trying to read from a windows CE comm port using C# and imported methods from coredll.dll, it seems...
3
by: Mauzi | last post by:
hi, this may sound odd and noob like, but what is the 'big' difference between static and non-static funcitons ? is there any performace differnce? what is the best way to use them ? thnx ...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
12
by: chandu | last post by:
hello, i want to know usage of static methods in a class. is it advantageous or disadvantage to use more static methods in a class. thank u
6
by: semkaa | last post by:
Can you explain why using ref keyword for passing parameters works slower that passing parameters by values itself. I wrote 2 examples to test it: //using ref static void Main(string args) {...
6
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.