简体   繁体   中英

How to call a function from another class to main class

I created another class (calculations) and created a function in it which checks if a number is even or odd. I want to call this function in my program class so it can check if variable (result) is even or odd.

I tried to call the method like: CheckEvenOrOdd(result).

class Program
{
    static void Main(string[] args)
    {
        int number1;
        int number2;
        int result;

        Console.Write("Enter a number: ");
        number1 = int.Parse(Console.ReadLine());

        Console.Write("Enter a second number: ");
        number2 = int.Parse(Console.ReadLine());

        result = number2 * number2;

        Console.WriteLine($"The total is: {result} ");
        Console.WriteLine("AND");

       // i tried this here but it doesn't work: CheckEvenOrOdd(result)

    }


}


class Calculations
{
    public static void CheckEvenOrOdd(int numb)
    {
        if (numb % 2 == 0)
        {
            Console.WriteLine("The number is even");

        }
        else
        {
            Console.WriteLine("The number is odd ");
        }

    }

}

Because your method is in a different class, you're going to have to make it static and then call it by class name first then method name. If it's not static you're going to have to instantiate a new instance of that class before being able to access any of its methods.

(btw you're multiplying number2 by number2, changed it for you as well in the codes provided below hehe)

Static:

class Program
{
    static void Main(string[] args)
    {
        int number1;
        int number2;
        int result;

        Console.Write("Enter a number: ");
        number1 = int.Parse(Console.ReadLine());

        Console.Write("Enter a second number: ");
        number2 = int.Parse(Console.ReadLine());

        result = number1 * number2;

        Console.WriteLine($"The total is: {result} ");
        Console.WriteLine("AND");

        Calculations.CheckEvenOrOdd(result);
        Console.ReadLine();
     }
}

public static class Calculations
{
    public static void CheckEvenOrOdd(int numb)
    {
        if (numb % 2 == 0)
        {
            Console.WriteLine("The number is even");
        }
        else
        {
            Console.WriteLine("The number is odd ");
        }
    }
}

Not Static:

class Program
{
    static void Main(string[] args)
    {
        int number1;
        int number2;
        int result;

        Console.Write("Enter a number: ");
        number1 = int.Parse(Console.ReadLine());

        Console.Write("Enter a second number: ");
        number2 = int.Parse(Console.ReadLine());

        result = number1 * number2;

        Console.WriteLine($"The total is: {result} ");
        Console.WriteLine("AND");

        Calculations calc = new Calculations();
        calc.CheckEvenOrOdd(result);
        Console.ReadLine();
     }
}

public class Calculations
{
    public void CheckEvenOrOdd(int numb)
    {
        if (numb % 2 == 0)
        {
            Console.WriteLine("The number is even");
        }
        else
        {
            Console.WriteLine("The number is odd ");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM