Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. gloabal variable

gloabal variable

Scheduled Pinned Locked Moved C#
csharpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kabutar
    wrote on last edited by
    #1

    hi , in this code i dont want to declare the result as a static gloabl variable..why are doing so and how can i avoid doing that... class Program { static int result; public int add(int x,int y) { result = x+y; return result; } static void Main(string[] args) { Program obj = new Program(); int sum = obj.add(10, 20); Console.WriteLine("{0}", result); Console.WriteLine("{0}", sum); Console.ReadLine(); } } } thanks

    C#

    A L 2 Replies Last reply
    0
    • K kabutar

      hi , in this code i dont want to declare the result as a static gloabl variable..why are doing so and how can i avoid doing that... class Program { static int result; public int add(int x,int y) { result = x+y; return result; } static void Main(string[] args) { Program obj = new Program(); int sum = obj.add(10, 20); Console.WriteLine("{0}", result); Console.WriteLine("{0}", sum); Console.ReadLine(); } } } thanks

      C#

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #2

      I thing from the output of your code you will get your ans :sigh:

      Happy Programming ----- Abhijit

      K 1 Reply Last reply
      0
      • A Abhijit Jana

        I thing from the output of your code you will get your ans :sigh:

        Happy Programming ----- Abhijit

        K Offline
        K Offline
        kabutar
        wrote on last edited by
        #3

        am sorry but i didnot understand what u said abhijith am a beginner and can u explain tis to me plzz thansk

        C#

        R 1 Reply Last reply
        0
        • K kabutar

          hi , in this code i dont want to declare the result as a static gloabl variable..why are doing so and how can i avoid doing that... class Program { static int result; public int add(int x,int y) { result = x+y; return result; } static void Main(string[] args) { Program obj = new Program(); int sum = obj.add(10, 20); Console.WriteLine("{0}", result); Console.WriteLine("{0}", sum); Console.ReadLine(); } } } thanks

          C#

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, your program does not need result to be static. It is used by add() only, which is not static, so it could access instance members. But your class does not need a data field result at all. It is used only inside add(), so make it a variable local to add(). BTW: once you fixed that, the add() method no longer uses any class members, so you COULD declare it static to make that clear. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


          1 Reply Last reply
          0
          • K kabutar

            am sorry but i didnot understand what u said abhijith am a beginner and can u explain tis to me plzz thansk

            C#

            R Offline
            R Offline
            rajansethi
            wrote on last edited by
            #5

            just replace add method with public int add(int x,int y) { return x+y; } And remove the global variable result from the code

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups