Results 1 to 6 of 6

Thread: Unable to call class into main method in c# code.

  1. #1
    Join Date
    Nov 2009
    Posts
    72

    Unable to call class into main method in c# code.

    Hello friends,
    I recently started learning C# code. I have write following code in c#, but it doesn't work. In this code I unable to call class into main method. I don't know what is the problem. I don't know why I unable to call class into main method in c# code. Please help me. For your information I have written following code for you.
    Code:
    using System;
    namespace AddMinusDivideMultiply
    {
        class ProgramEg
        {
            public static int x, y;
    
            public static void Main()
            {
    
                Console.Write("Please Enter 1st Number  :");
                string temps = Console.ReadLine();
                x = Int32.Parse(temps);
    
                Console.Write("Please Enter 2nd Number :");
                temps = Console.ReadLine();
                j = Int32.Parse(temps);
    
                Minuz.minus(); 
    
            }
        }
    
        class Terms
        {
            public static void Add()
            {
                int adds;
                adds = Programeg.x+ Programeg.y;
                Console.WriteLine(" First and The Second Number is {0}", add);
            }

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Unable to call class into main method in c# code.

    You have made small mistake in your code and that's why you are getting such type of problem. You have written "minuz" and that's why you are getting such type of problem. In your code you have to call this:
    Code:
    Minuz.Minus();
    After this you also have to make some changes in your code as follows:
    Code:
    class TermsWg
    {
        public static void Add()
        {
            int adds;
            adds = Programs.x + Programs.y;
            Console.WriteLine("First and The Second Number is {0}", adds);
        }
    }
    
    class Minuz
    {
        public static void Minus()
        {
        int minuss;
        minuss = Programs.x - Programs.y;
        Console.WriteLine("The Subraction is {0}", minus);
        }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Unable to call class into main method in c# code.

    You have declared Minuz class into the class Terms and it has private access specifier. It means that this class is not visible from the Main method. You can fix this problem in following two ways.
    1.You have to make Minuz class public or internal and after that you have to call Minus method to Terms.Minuz.Minus().
    2.You have to declare Minuz class outside of the Terms class so that it will be declared in the namespace.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Unable to call class into main method in c# code.

    You have made some small mistake in your code and that's why you are such type of problem. In your code you have embedded the Minuz class into the Terms class, which is wrong way to declare class. To fix this problem you have to make Minuz class public in following ways.
    Code:
    Terms.Minuz.Minus();
    After this you have to move Minuz class out of Terms.

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Unable to call class into main method in c# code.

    You are not able to call class into main method in your c# code, because Minuz class is define in the Terms class. This is wrong way to define such type of class. You also haven't close the definition of Terms class before declaring Minuz class and that's why you are not able to call class into main method. In this case you have to first close the definition of Terms class to fix this problem.

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Unable to call class into main method in c# code.

    I think you have accidentally forget to write closing bracket after the Terms class and that's why you are getting such type of problem. To fix this problem you have to first write closing bracket after the Terms class. After this you have to write following code into Main method to get rid out of this problem.
    Code:
    Terms.Minuz.Minus();

Similar Threads

  1. How to call a method in another class in Java
    By kannudhal in forum Software Development
    Replies: 4
    Last Post: 28-10-2010, 09:29 AM
  2. Write Code To Execute Before Main Method
    By rashmi_ay in forum Software Development
    Replies: 5
    Last Post: 20-01-2010, 12:45 PM
  3. Is it possible to call destroy() method within init() Method?
    By Level8 in forum Software Development
    Replies: 3
    Last Post: 10-12-2009, 08:36 AM
  4. How to pass arguments to the main method
    By Wannabe in forum Software Development
    Replies: 3
    Last Post: 12-09-2009, 11:51 AM
  5. What if we declare the main method as private?
    By Baran in forum Software Development
    Replies: 4
    Last Post: 25-02-2009, 07:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,412,108.22685 seconds with 17 queries