why ths code is not working
-
using System; class test { private static int add(int x,int y) { return x+y; } public int calling(int x, int y) { int k; test obj=new test(); k=obj.add(x,y); return k; } } class now { public static void Main() { int sum,x,y; Console.WriteLine("enter x -->> "); x=int.Parse(Console.ReadLine()); Console.WriteLine("enter y -->> "); y=int.Parse(Console.ReadLine()); test obj1=new test(); sum=obj1.calling(x,y); Console.WriteLine("the value is --> {0}",sum); } }
-
using System; class test { private static int add(int x,int y) { return x+y; } public int calling(int x, int y) { int k; test obj=new test(); k=obj.add(x,y); return k; } } class now { public static void Main() { int sum,x,y; Console.WriteLine("enter x -->> "); x=int.Parse(Console.ReadLine()); Console.WriteLine("enter y -->> "); y=int.Parse(Console.ReadLine()); test obj1=new test(); sum=obj1.calling(x,y); Console.WriteLine("the value is --> {0}",sum); } }
Define "not working."
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
using System; class test { private static int add(int x,int y) { return x+y; } public int calling(int x, int y) { int k; test obj=new test(); k=obj.add(x,y); return k; } } class now { public static void Main() { int sum,x,y; Console.WriteLine("enter x -->> "); x=int.Parse(Console.ReadLine()); Console.WriteLine("enter y -->> "); y=int.Parse(Console.ReadLine()); test obj1=new test(); sum=obj1.calling(x,y); Console.WriteLine("the value is --> {0}",sum); } }
1. You don't need to create an instance of class test to use the add function, since add is a static function. int sum = test.add(1, 7); If the add function was not a static function, you would need to create an instance of test: test myTest = new test(); int sum = myTest.add(1, 7); 2. the way you have it coded, the calling function is useless. You may have intended to make calling static, not add. Review static objects in classes.
-
using System; class test { private static int add(int x,int y) { return x+y; } public int calling(int x, int y) { int k; test obj=new test(); k=obj.add(x,y); return k; } } class now { public static void Main() { int sum,x,y; Console.WriteLine("enter x -->> "); x=int.Parse(Console.ReadLine()); Console.WriteLine("enter y -->> "); y=int.Parse(Console.ReadLine()); test obj1=new test(); sum=obj1.calling(x,y); Console.WriteLine("the value is --> {0}",sum); } }
It doesn't work because you're calling static method add() using object instance. You needn't create object to call add() method:
public int calling(int x, int y)
{
return add(x,y);
}And you even needn't create object to sum up your numbers:
public static void Main()
{
Console.WriteLine("enter x -->> ");
int x = int.Parse(Console.ReadLine());Console.WriteLine("enter y -->> "); int y = int.Parse(Console.ReadLine()); int sum = test.add(x,y); Console.WriteLine("the value is --> {0}",sum);
}
-
1. You don't need to create an instance of class test to use the add function, since add is a static function. int sum = test.add(1, 7); If the add function was not a static function, you would need to create an instance of test: test myTest = new test(); int sum = myTest.add(1, 7); 2. the way you have it coded, the calling function is useless. You may have intended to make calling static, not add. Review static objects in classes.
the method add and the entry point method main() are in different classes so an object will be required to call add. not exactly an object but add will be called using the class name instead of the object name. further the add is a private method and it is being called through the public method calling. now method calling is also in a different class from the class in which entry point is. now to use calling we have to create an object but it is not working. i don't know why. please look at it once more.