1

So I'm going to simplify an assignment I'm working on. I'm sorry if it's crazy bad I'm very new to C#. I should add that the finalMethod call is within Main() and is the only thing in Main().

finalMethod(ifPossible(functionOne()), functionTwo()))

static int functionOne()
    {
        int number;
        Console.WriteLine("Enter a number: ");
        number = int.Parse(Console.ReadLine());
        return number;
    }

static int functionTwo()
    {
        int number;
        Console.WriteLine("Enter a number: ");
        number = int.Parse(Console.ReadLine());
        return number;
    }

static bool ifPossible(int x, int y)
    {
        if (x < y)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

static int finalMethod(bool x)
    {
        if (x == true)
        {
            Console.Write("Success");
        }
        else
        {
            Console.Write("Fail");
        }
    }

My problem is that I need to return the int values from the first two functions into the finalMethod function. This is probably going to require a lot of restructuring but any help would be greatly appreciated.

1
  • It's not clear what you're asking. Why not call ifPossible() from finalMethod() and just pass both x and y to finalMethod()? There are numerous answers on Stack Overflow already discussing how to return multiple values from a single method, but it doesn't really seem like that's even needed here. What have you tried? What specifically did you have trouble with? Commented Sep 8, 2016 at 3:07

2 Answers 2

1

You have a close ) on a wrong place. Try this:

finalMethod(ifPossible(functionOne(), functionTwo()));

Try to do some refactoring like this:

static int getInputValue()
{
    Console.WriteLine("Enter a number: ");
    var input = Console.ReadLine();
    return int.Parse(input);
}

static bool ifPossible(int x, int y)
{
    return x < y;
}

static void finalMethod(bool x)
{
    if (x)
    {
        Console.Write("Success");
    }
    else
    {
        Console.Write("Fail");
    }
}

var number1 = getInputValue();
var number2 = getInputValue();

finalMethod(ifPossible(number1, number2))
Sign up to request clarification or add additional context in comments.

Comments

0

Couple of things in above code: 1.remove int return value from finalmethod 2. ifpossible is taking 1 argument it should take two.

    class Program
    {
        static void Main(string[] args)
        {
            SomeClass.finalMethod(SomeClass.ifPossible(SomeClass.functionOne(), SomeClass.functionTwo()));
            Console.ReadLine();
        }
}
    class SomeClass
{
    internal static int functionOne()
    {
        int number;
        Console.WriteLine("Enter a number: ");
        number = int.Parse(Console.ReadLine());
        return number;
    }

    internal static int functionTwo()
    {
        int number;
        Console.WriteLine("Enter a number: ");
        number = int.Parse(Console.ReadLine());
        return number;
    }

    internal static bool ifPossible(int x, int y)
    {
        if (x < y)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    internal static void finalMethod(bool x)
    {
        if (x)
        {
            Console.Write("Success");
        }
        else
        {
            Console.Write("Fail");
        }
    }
}

Comments

Your Answer

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