Lithicas
Lithicas

Reputation: 4013

C# - Accessing methods from another class within other classes?

I have recently started my journey to learn C# so I'm in no way an expert. Anyways, I tried to create an object-oriented calculator but I've run in to a problem where it seems like I can't access methods from other classes within certain classes. I get no error codes but when I put a breakpoint down and debug the application the value of the variables are 0.

If I remove the method from DisplayUserData and paste the code in my main program file, it runs fine. It seems like the DisplayUserData class can't pass the variables or retrieve variables from other classes, why is this?

Oh and, excuse the horrible method, class and variable names.

Here is my main program file:

namespace Calculator
{
    class Program
    {
        static void Main(string[] args)
        {
            GetUserInput userInput = new GetUserInput();
            Calculator calculator = new Calculator();
            DisplayUserData displayUserCalculation = new DisplayUserData();


            userInput.MethodOfCalculation();
            userInput.UserNumberInput();
            displayUserCalculation.DisplayCalculatedUserData();

        }
    }
}

Followed by my GetUserInput class:

namespace Calculator
{
    public class GetUserInput
    {
        public char Choice;
        public decimal ValueOne;
        public decimal ValueTwo;

        public void MethodOfCalculation()
        {
            Console.WriteLine("Welcome to Calculator v.1!");
            Console.WriteLine("Please choose a method of calculation below.");
            Console.WriteLine("1: Addition");
            Console.WriteLine("2: Subtraction");
            Console.Write("Your choice: ");
            Choice = Console.ReadKey().KeyChar;
        }

        public void UserNumberInput()
        {
            Console.Clear();
            Console.WriteLine("Please enter two numbers for calculation!");
            Console.Write("Number One: ");
            ValueOne = Convert.ToDecimal(Console.ReadLine());
            Console.Write("Number Two: ");
            ValueTwo = Convert.ToDecimal(Console.ReadLine());
        }

    }
}

And the Calculator class:

namespace Calculator
{
    public class Calculator
    {
        public decimal CalculateUserInput(char userChoice, decimal valueOne, decimal valueTwo)
        {
            decimal calculatedValue = 0;

            switch (userChoice)
            {
                case '1':
                    calculatedValue = valueOne + valueTwo;
                    break;
                case '2':
                    calculatedValue = valueOne - valueTwo;
                    break;
            }

            return calculatedValue;
        }
    }
}

And lastly the DisplayUserData class:

namespace Calculator
{
    class DisplayUserData
    {
        GetUserInput _userInput = new GetUserInput();
        Calculator _calculator = new Calculator();

        public void DisplayCalculatedUserData()
        {
            decimal calculatedValue = _calculator.CalculateUserInput(_userInput.Choice, _userInput.ValueOne, _userInput.ValueTwo);

            switch (_userInput.Choice)
            {
                case '1':
                    Console.WriteLine("Addition: " + _userInput.ValueOne + " + " + _userInput.ValueTwo + " = " + calculatedValue);
                    break;
                case '2':
                    Console.WriteLine("Subtraction: " + _userInput.ValueOne + " - " + _userInput.ValueTwo + " = " + calculatedValue);
                    break;
            }
        }
    }
}

Upvotes: 0

Views: 1013

Answers (2)

Silas Reinagel
Silas Reinagel

Reputation: 4203

Your DisplayUserData class has references to different Calculator and GetUserInput classes than the ones that your Program refers to. You should pass the same instances to DisplayUserData like this.

class DisplayUserData
{
    readonly GetUserInput _userInput;
    readonly Calculator _calculator;

    public DisplayUserData(Calculator calculator, GetUserInput userInput)
    {
        _calculator = calculator;
        _userInput = userInput;
    }

    public void DisplayCalculatedUserData()
    {
        ...
    }
}

Program would then be:

class Program
{
    static void Main(string[] args)
    {
        GetUserInput userInput = new GetUserInput();
        Calculator calculator = new Calculator();
        DisplayUserData displayUserCalculation = new DisplayUserData(calculator, userInput);

        userInput.MethodOfCalculation();
        userInput.UserNumberInput();
        displayUserCalculation.DisplayCalculatedUserData();

    }
}

Upvotes: 3

tvanfosson
tvanfosson

Reputation: 532465

You are creating new versions (instances) of the UserInput and Calculator classes in your DisplayUserData class. Because you were using new instances, those instances didn't have access to the data contained in the instances used in your main method. Instead of creating the new instances in your class, pass the existing instances in through a constructor for the class to use.

namespace Calculator
{
    class DisplayUserData
    {
        private readonly GetUserInput _userInput;
        private readonly Calculator _calculator;

        DisplayUserData(GetUserInput userInput, Calculator calculator)
        {
            _userInput = userInput;
            _calculator = calculator;
        }

        public void DisplayCalculatedUserData()
        {
            decimal calculatedValue = _calculator.CalculateUserInput(_userInput.Choice, _userInput.ValueOne, _userInput.ValueTwo);

            switch (_userInput.Choice)
            {
                case '1':
                    Console.WriteLine("Addition: " + _userInput.ValueOne + " + " + _userInput.ValueTwo + " = " + calculatedValue);
                    break;
                case '2':
                    Console.WriteLine("Subtraction: " + _userInput.ValueOne + " - " + _userInput.ValueTwo + " = " + calculatedValue);
                    break;
            }
        }
    }
}

Then call it as

displayUserCalculation.DisplayCalculatedUserData(userInput, calculator);

Upvotes: 5

Related Questions