Search code examples
c#return

C# How to return a value between 2 .cs files


So i am trying to return the discount amount between 2 .cs files into the main and print out the amount there instead of doing it on the second class. I am pretty new at this and i need help

code is not yet complete

MAIN

using System;

    namespace CalcDiscount
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Enter price");
                double input = double.Parse(Console.ReadLine());
                    Calculator myCalculator = new Calculator();
                myCalculator.Calculation(input);

                  Console.WriteLine("Enter discount");
                input = double.Parse(Console.ReadLine());
                Console.WriteLine("");
                Console.ReadLine();
            }
        }
    }

SECOND FILE calculator.cs

using System;

    namespace CalcDiscount
    {
        public class Calculator
        {
            public void Calculation(double input)
            {
                Console.WriteLine("Your entered the number: " + input);
                int i = 1;
                if (input != 0)
                {
                     Console.WriteLine(input + " x " + i + " = " + input * i);

                }

            }
        }
    }

Solution

  • You could change the method Calculation in your Calculator class, from void to double. The method will calculate the result and return it to the main function, where it will be printed.

    Calculation method:

    public double Calculation(double input1, double input2)
    {
          return (input1 * input2);
    }
    

    Main:

     Console.WriteLine("Enter first input");
     double input1 = double.Parse(Console.ReadLine());
     Console.WriteLine("Enter second input");
     double input2 = double.Parse(Console.ReadLine());
     Calculator myCalculator = new Calculator();
     double result = myCalculator.Calculation(input1, input2);
     Console.WriteLine("result = " + result);