Hi,
Im aware parameters within a parenthesis are by default input parameters.
And I know that by placing out in front of the type of each parameter delcares it as an output, but Im not understanding exactly how and why these are used surely the return would do this job?
Regards,
Loading
Guest UserPosted Apr 5, 2011, 10:35 AM
However there may be occasions where you would need to declare a method as return-type void, in which case an output parameter would be the only way to get a return value.
Posted Apr 6, 2011, 8:02 AM
John's "Accepted Answer" restored.
VulpesPosted Apr 6, 2011, 7:57 AM
public double GetInvestment(string sPrompt){ ... }
Also a couple of further points about 'out' parameters:-
1. Unlike 'ref' parameters, it's not necessary to initialize the variable. So, in Raja's code, he could have done:
double dvalue;
2. The compiler guarantees that the 'out' parameter will have been assigned a value by the time the method returns.
P.S. You've taken away John's 'Accepted Answer' ! It's a good idea to ask a new question on a new thread so other posters such as Raja can be given credit for answering it correctly.
Posted Apr 6, 2011, 7:56 AM
Raja KrishnamurthyPosted Apr 6, 2011, 7:34 AM
This method is accepting\passing in 1 input parameter "double dvalue" and 1 output parameter "string sPrompt".
I think you got this wrong. He is passing two parameters of which sPrompt is the input while he expects the output of the method GetInvestment to be populated into dvalue which is the output parameter hence the keyword "out" is prefixed to the parameter.
He would now invoke the method something like this:
double dvalue; //Good point on the assignment Vulpes corrected :-)
string sPrompt = "some input value";
GetInvestment(out dvalue,sPrompt);
Console.WriteLine("Investment is : {0}", dvalue); //displaying the calculated investment
Also to get a better understanding of methods and parameters check out the recommended articles section.
HTH.
Posted Apr 6, 2011, 7:18 AM
Given the below line of code I sourced from another articles answer of yours...
public void GetInvestment(out double dvalue, string sPrompt)
This method is accepting\passing in 1 input parameter "double dvalue" and 1 output parameter "string sPrompt".
Am I write in saying that the point of giving this method the 2 parameters within the parentheses rather than the body of the method is so when the method is called the user\caller must insert the corresponding arguments of which then the triggering to initialize the components is defined and referenced in the body...
Sam HobbsPosted Apr 5, 2011, 4:59 PM
Guest UserPosted Apr 5, 2011, 11:04 AM
VulpesPosted Apr 5, 2011, 10:59 AM
Posted Apr 5, 2011, 10:51 AM
Im torn as to who to give the Accepted answer to?
VulpesPosted Apr 5, 2011, 10:41 AM
int num;
string s = "42";
bool isValid = Int32.TryParse(s, out num); // return type of bool
if (isValid)
{
Console.WriteLine("The number is {0}", num); // 42
}
else
{
Console.WriteLine("The number was invalid but has been given a default value of {0}", num); // default is zero
}
VulpesPosted Apr 5, 2011, 10:32 AM
In C# 4.0 you can also use a Tuple for this purpose.