Using Static Methods Adv?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chandu

    #1

    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


  • james

    #2
    Re: Using Static Methods Adv?


    "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 ?
    >
    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.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.

    James


    Comment

    • Jianwei Sun

      #3
      Re: Using Static Methods Adv?

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

      Comment

      • John Duval

        #4
        Re: Using Static Methods Adv?

        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

        Comment

        • Stoitcho Goutsev \(100\)

          #5
          Re: Using Static Methods Adv?

          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

          • Arne Vajhøj

            #6
            Re: Using Static Methods Adv?

            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

            Comment

            Working...