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. why ths code is not working

why ths code is not working

Scheduled Pinned Locked Moved C#
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.
  • D Offline
    D Offline
    digitalangel
    wrote on last edited by
    #1

    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); } }

    D H A 3 Replies Last reply
    0
    • D digitalangel

      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); } }

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • D digitalangel

        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); } }

        H Offline
        H Offline
        Heywood
        wrote on last edited by
        #3

        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.

        D 1 Reply Last reply
        0
        • D digitalangel

          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); } }

          A Offline
          A Offline
          Andrew Lygin
          wrote on last edited by
          #4

          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 Reply Last reply
          0
          • H Heywood

            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.

            D Offline
            D Offline
            digitalangel
            wrote on last edited by
            #5

            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.

            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