Home > Net >  C#, How to return a string and Int from the same method
C#, How to return a string and Int from the same method

Time:10-09

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;
}

CodePudding user response:

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.")
    };

CodePudding user response:

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.

CodePudding user response:

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(); 
                            } 
                }
        }
}

CodePudding user response:

Assuming that we dont use switch and your code has the least change

try this:

        Console.WriteLine(GetResult(num1, num2, op));

       to

        var res = GetResult(num1, num2, op);
        Console.WriteLine(res == null? "Invalid Operator" : res);

and your function:

static Nullable<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
            {
                return null;
            }
            return result;
        }

CodePudding user response:

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);
  •  Tags:  
  • c#
  • Related