gloabal variable
-
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#
-
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#
I thing from the output of your code you will get your ans :sigh:
Happy Programming ----- Abhijit
-
I thing from the output of your code you will get your ans :sigh:
Happy Programming ----- Abhijit
-
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#
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
-
am sorry but i didnot understand what u said abhijith am a beginner and can u explain tis to me plzz thansk
C#
just replace add method with public int add(int x,int y) { return x+y; } And remove the global variable result from the code