3D-kreativ
3D-kreativ

Reputation: 9319

Return value of parameterized methods?

I'm new to C# and I'm not sure I understand the use of parameterized methods. Below I have added some code that I have been given in a task that I'm going to develop. It's for a simple GUI with some text boxes asking for a name and a price.

The first line of code calls the method and the boolean variable inputOk expects a true or false value and the out parameter will also "return" some values?

In the second line of code, I guess, despite the "return" of name and price I also need to have a return true or false to get the first line of code to work? Just want to be sure I understand it. And since I can reach the input value from the text boxes like txtName.Text, I don't need to add this value when I call the methods? Thanks!

bool inputOk = ReadAndValidateInput(out customerName, out seatPrice);

private bool ReadAndValidateInput(out string name, out double price)

Upvotes: 1

Views: 144

Answers (3)

Andras Zoltan
Andras Zoltan

Reputation: 42363

Simply put, yes you are right on all counts, but I do think you might need to brush up on your terminology; the return value of a method is what's returned while the stuff that goes in the brackets are parameters; and when you call the the method they are called arguments.

For some more detail.

It's best to start with the second line.

The boolean is returned from the method. The name and price parameters are guaranteed to be modified by the method (because they are out, if they were ref then they might be modified); and, while yes they can be thought of as additional return values, in reality the mechanism is completely different: they are just called output parameters.

Edit - concerning 'output parameters'

An output parameter can still used also to pass in values (so really they are input/output). The method that receives the argument must then ensure that it then writes to it (because it's out).

End-edit

If that method is written inside a form class, which owns a textbox then, yes, you can simply use the textbox variable without having to pass it in; because the method is an 'instance method' (as opposed to a static, which has no this) and the variable belongs to the same instance of the form.

On the first line, yes - inputOk receives the boolean return value from calling the method - passing customerName and seatPrice as output arguments. After the method returns, assuming no exception occurs, inputOk will be set to the return value of the method; and the two arguments will receive the values set by the ReadAndValidateInput method call.

Upvotes: 1

Michel Keijzers
Michel Keijzers

Reputation: 15377

You normally should call it as:

string name;
double price;
bool inputOk = ReadAndValidateInput(out customerName, out seatPrice);

All three variables will get assigned values within ReadAndValidateInput. In C++ it is not possible to return multiple values and the 'out' keyword is then an alternative to use.

However, it is best to avoid it if possible. You also can make two additional get functions to return the customer name and seat price.

Upvotes: 1

archil
archil

Reputation: 39501

Imagine situation, where ReadAndValidateInput would not only validate input, but do some calculations and return other value - discount for example. That's where you would need out parameter

double discount = 0;

if(ReadAndValidateInput(customerName, seatPrice, out discount))
{
   //do something with discount. You know that input was valid
}
else
{  // do not touch discount. User has not entered valid values
}

You do not need out parameters if method being called does not change values or does not generate new values other than return ones. You are right - original values can be accessed from calling code.

Upvotes: 1

Related Questions