0

How do I return a string from a Int method? Please see my code below. In the last method I would like to print the string "Invalid Operator" if the use keyed in a wrong operator

    static void Main(string[] args)
    {
        Console.WriteLine("Welcome to the simple calculator");
        Console.WriteLine("-------------");
        Console.WriteLine("Enter the first number");
        int num1 = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter the Second number");
        int num2 = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Select the Operator");
        Console.WriteLine("Key in on of the following numbers:\n" + "1 to add \n" + "2 to subtract \n" + "3 to multiply \n" + "4 to divide");
        int op = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Your result is");
        Console.WriteLine("---------------");
        Console.WriteLine(GetResult(num1, num2, op));

        Console.ReadLine();
    }

I would like the below method to return both the String and Int values

static int GetResult(int a, int b, int c)
{
    int result;

    if (c == 1)
    {
        result = a + b;
    }
    else if (c == 2)
    {
        result = a - b;
    }
    else if (c == 3)
    {
        result = a * b;
    }
    else if (c == 4)
    {
        result = a / b;
    }
    else
    {
        result = "Invalid Operator"; 
    } 

    return result;
}
5
  • Edit your question instead of adding the code into comment Commented Oct 8, 2022 at 10:20
  • 1
    You are probably looking for ValueTuple Commented Oct 8, 2022 at 10:22
  • In this scenario I'd make the method signature static string GetResult(int a, int b, int c, out rersult). Then you could print your string result while also getting the numerical value. Although the use case doesn't make sense, hopefully there's a bigger purpose. Commented Oct 8, 2022 at 10:28
  • 1
    I think what you are actually looking for is the concept of exceptions, since you want to return an int value if the passed operator is expected and throw an exception of "invalid operator" if it is not. Commented Oct 8, 2022 at 10:34
  • @YuriyFaktorovich I added that, but I am getting an error > static int GetResult(int a, int b, int c, out result) { int result; if (c == 1) { result = a + b; } else if (c == 2) { result = a - b; } else if (c == 3) { result = a * b; }else if (c==4) { result = a / b; } else { result = "Invalid Operator"; } return result; } Commented Oct 8, 2022 at 10:39

4 Answers 4

5

You could use tuples, but I think an exception would be better here, e.g.

static int GetResult(int operatorCode, int operand1, int operand2) =>
    operatorCode switch
    {
        1 => operand1 + operand2,
        2 => operand1 - operand2,
        3 => operand1 * operand2,
        4 => operand1 / operand2,
        _ => throw new ArgumentOutOfRangeException(nameof(operatorCode), $"operatorCode {operatorCode} is not valid.")
    };
Sign up to request clarification or add additional context in comments.

1 Comment

In this case I'd use throw new ArgumentOutOfRangeException(nameof(operatorCode), $"operatorCode {operatorCode} is not valid."); since the error is indeed caused by an out-of-range argument.
0

I would recommend exceptions to handle invalid input.

However, you could also do one of these things:

Option 1: Value Tuple

static (int?, string) GetResult(int a, int b, int c)
{
    int? result = null;
    string error = null;

    if (c == 1)
    {
        result = a + b;
    }
    else if (c == 2)
    {
        result = a - b;

    }
    else if (c == 3)
    {
        result = a * b;
    }
    else if (c == 4)
    {
        result = a / b;
    }
    else
    {
        error = "Invalid operator";
    }

    return (result, error);
}

Option 2: Enum

public enum OperatorCode
{
   Add,
   Sub,
   Mul,
   Div
}
static int GetResult(OperatorCode operatorCode, int operand1, int operand2) =>
    operatorCode switch
    {
        OperatorCode.Add => operand1 + operand2,
        OperatorCode.Sub => operand1 - operand2,
        OperatorCode.Mul => operand1 * operand2,
        OperatorCode.Div => operand1 / operand2,
    };

This second option doesn't require an error message, because only valid operator codes can be passed to the method.

Comments

0

You can try this

'''

using System;

namespace example 
{
    public static class Program
        {
            static void Main(string[] args)
                {
                    Console.WriteLine("Welcome to the simple calculator");
                    Console.WriteLine("-------------");
                    Console.WriteLine("Enter the first number");
                    int num1 = Convert.ToInt32(Console.ReadLine());;
                    Console.WriteLine("Enter the Second number");
                    int num2 = Convert.ToInt32(Console.ReadLine());;
                    Console.WriteLine("Select the Operator");
                    Console.WriteLine("Key in on of the following numbers:\n" + "1 to add \n" + "2 to subtract \n" + "3 to multiply \n" + "4 to divide");
                    int op = Convert.ToInt32(Console.ReadLine());;
                    GetResult(num1, num2, op);
                }
            static void Invalid()
                {
                    Console.WriteLine("Invalid Operator, Try Again");
    
                }

            static void GetResult(int a, int b, int c)
                {
                    int result;
                    string pref = "Your result is: ";
                        if (c == 1)
                            {
                                result = a + b;
                                Console.WriteLine(pref+result);
                            }
                        else if (c == 2)
                            {
                                result = a - b;
                                Console.WriteLine(pref+result);
                            }
                        else if (c == 3)
                            {
                                result = a * b;
                                Console.WriteLine(pref+result);
                            }
                        else if (c==4)
                            {
                                result = a / b;
                                Console.WriteLine(pref+result);
                            }
                        else
                            {
                                Invalid(); 
                            } 
                }
        }
}

Comments

-1

You could change the return type from int to object

static object  GetResult(int a, int b, int c)
{
    object result;
    if (c == 1)
    {
        result = a + b;
    }
    else if (c == 2)
    {
        result = a - b;

    }
    else if (c == 3)
    {
        result = a * b;
    }
    else if (c == 4)
    {
        result = a / b;
    }
    else
    {
        result = "Invalid Operator";
    }
    return result;
}

I have never used objects for big projects. It is better to know the return type.

Or you could print while doing the operation. make the method void.

 Console.WriteLine(a +b);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.