-6

How can I solve this problem? (the text at the end "brah I've told...). The program want's that I give an int back, but I want to give a string back. How can I solve this problem? Because I've told it that it will be an int returned, but I want to have this failure message. Thank you very much guys!!

edit the problem exactly is, that the program won't compile because of else return Console.WriteLine("brah I've told ya that I want more than 5"); Because my program wants a int, no string.

static void Main(string[] args)
{
    Console.WriteLine("Give me a number over 5 bro");
    int x = int.Parse(Console.ReadLine());

    int result = AddNumbers(x, 5);
    Console.WriteLine(result);

    Console.WriteLine("Press enter to close...");
    Console.ReadLine();
}

public static int AddNumbers(int number1, int number2)
{
    int result = number1 + number2;

    if (result > 10)
    {
        return result;
    }
    else return Console.WriteLine("brah I've told ya that I want more than 5");
}
}
}
pushkin
  • 9,575
  • 15
  • 51
  • 95
Juri
  • 9
  • 5
  • If the method says it is returning an `int`, it has to return an `int`, period. Perhaps you need a separate method (for example, named `ValidateResult()`) that checks the result after it has been computed. Such a method could return a string or display one using the console. – John Wu Jun 06 '18 at 20:32
  • Or you can study this question [How can I return multiple values from a function?](https://stackoverflow.com/questions/748062/how-can-i-return-multiple-values-from-a-function-in-c) – Steve Jun 06 '18 at 20:34
  • 1
    `Console.WriteLine` is `void`, so you can't return it anyway. This code doesn't compile. – Daniel Mann Jun 06 '18 at 20:37
  • 1
    I think you need to restate your question and desired outcome. Based on the code you provided, the "else" in question seems more like an exception than a return value. – user1011627 Jun 06 '18 at 20:39
  • Just check if `x` is greater than 5 before you call `AddNumbers`, then this whole problem goes away. – Kirk Larkin Jun 06 '18 at 20:43
  • @user1011627 While you are correct; trying to get into exception handling might be a bit much for the OP right now... – BradleyDotNET Jun 06 '18 at 20:46

5 Answers5

2

You can only have one return type from a method; and all return statements must honor it. That return type can be an object, one way to return "multiple" things: How can I return multiple values from a function in C#?

So you can't write what you want; at least not directly. You can return an int, a string, or an object containing both.

Your current code is nonsense anyways; as Console.WriteLine returns void. You can't return void (as a value, you can as a return type of course).

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
0

so working within the compounds of how c# works as a programming language, you can't have a method signature returning an int and then return a string. Now, thinking about this from a logical perspective, this is an error. The user did not enter a number over 5, so throw an exception and catch it. Here is how you would do this.

static void Main(string[] args)
{
    int result = 0;
    Console.WriteLine("Give me a number over 5 bro");
    int x = int.Parse(Console.ReadLine());

    try{

       result = AddNumbers(x, 5);
       Console.WriteLine(result);

    }catch(Exception e)
    {
       Console.WriteLine(e.Message);
    }


    Console.WriteLine("Press enter to close...");
    Console.ReadLine();


}    



public static int AddNumbers(int number1, int number2)
{
    int result = number1 + number2;

    if (result > 10)
    {
        return result;
    }
    else throw new Exception("brah I've told ya that I want more than 5");
}
Jaime Reynoso
  • 146
  • 1
  • 8
-1

Just return a string

public static string AddNumbers(int number1, int number2)
{
    int result = number1 + number2;

    if (result > 10)
    {
        return result.ToString();
    }
    else
    { 
        return "brah I've told ya that I want more than 5";
    }  
}
paparazzo
  • 44,497
  • 23
  • 105
  • 176
-1

Easiest and most logical way is to just keep the return type of AddNumbers method as integer as that makes sense, and either have a default return of 0 after writing to console like this:

   if (result > 10)
        {
            return result;
        }
        else
        {
        Console.WriteLine("brah I've told ya that I want more than 5");
        return 0;
        }

Or change return type to Int32? which allows you to return null by doing this:

public static Int32? AddNumbers(int number1, int number2)
    {
        int result = number1 + number2;
    if (result > 10)
    {
        return result;
    }
    else
    {
    Console.WriteLine("brah I've told ya that I want more than 5");
    return null;
    }
}

and don't forget variable type:

Int32? result = AddNumbers(x, 5);

Also make sure to check if result is 0 or null to change the resulting writeline message.

This wasn't in the question but you could keep asking user until they enter the right thing like this:

static void Main(string[] args)
    {
        Int32? result = null;
        while (result is null)
        {
            Console.WriteLine("Give me a number over 5 bro");
            int x = int.Parse(Console.ReadLine());
            Int32? result = AddNumbers(x, 5);
            if (result is null) continue;

            Console.WriteLine(result);
            Console.WriteLine("Press enter to close...");
            Console.ReadLine();
        }

    }
waleed
  • 451
  • 3
  • 11
-5

A better way to achieve what you're trying to do overall would be to use an exception. Consider if we wrote AddNumbers as such:

public static int AddNumbers(int number1, int number2)
{
    int result = number1 + number2;
    if(result <= 10)
    {
        throw new Exception("Should be more than 10");
    }
    return result;
}

and our main logic as:

    try
    {
        int result = AddNumbers(3, 3);
    }catch(Exception e)
    {
        Console.WriteLine(e.Message); // must've been > 10
        return; 
    }

The way that this works is, when AddNumbers encounters behavior that is not expected or allowable, it will throw an Exception. When we call AddNumbers, now, we must do it in a try-catch block in order to catch this Exception, if it gets thrown. This approach is cleaner and less convoluted than using a Tuple, which is essentially reinventing the wheel in this scenario.

So, if our numbers add to less than 10, AddNumbers will throw an exception, which is then caught in our main logic and logged to the console, printing "Should be more than 10".

Another option would be to simply change your main logic to be:

    int result = AddNumbers(x, 5);

    if(result > 10)
    {
        Console.WriteLine(result);
    }
    else
    {
        Console.WriteLine("You should have given me more than 5!");
    }

This has the advantage of being much simpler, but the disadvantage that we must remember to include this if statement every time we use AddNumbers. The Exception route is better design if we want to add more checks, for instance if you wanted to also require that the result be less than 30, divisible by 2, etc, you can just keep adding new thrown exceptions. However, if you only need to check if the result is greater than 10, it may be simpler overall to use this final option.

N.D.C.
  • 1,601
  • 10
  • 13
  • 4
    This doesn't seem like a good answer to me and seems to only confuse the issue. A method called "AddNumbers" should not return a Tuple just to allow a string exception to be passed back. I would not recommend this approach, even if it "works". – user1011627 Jun 06 '18 at 20:41
  • @user1011627 Not at all, the user wants to know how to return multiple things from a function, and this is an example of that. We can split hairs about better ways to design the code all day long, but the question isn't about whether he should be using an exception instead. – N.D.C. Jun 06 '18 at 20:44
  • I didn't downvote your answer so apparently I'm not the only one to disagree with you...but you got your points even though you are leading someone down the wrong path. – user1011627 Jun 06 '18 at 20:45
  • 5
    @NickDechiara When trying to help out beginners, it's helpful to them to give them answers that encourage good practices. This answer might solve the problem, but it's still not a good solution to the problem. It's overcomplicating something that can be solved by better separation of concerns or by implementing exception handling. *Those* are excellent practices. – Daniel Mann Jun 06 '18 at 20:47
  • 2
    `Tuple` is an evil, evil class. Lets not try to spread around *too* much... – BradleyDotNET Jun 06 '18 at 20:47
  • This is definitely not the way I'd advise a beginning programmer to solve this problem. If somebody on my team wrote it this way I'd send the code review back as "Needs Work." – John Wu Jun 06 '18 at 20:56
  • @DanielMann RE: your concerns I have updated the post – N.D.C. Jun 06 '18 at 21:01