Answered by:
Passing reference parameter to return the value?

Question
-
I've got a program that is to be written using three different methods, one with a return type, one without a return type one with a reference parameter to return the value.
The first two are set up like this:
private: System::Void btnConvert_Click(System::Object^ sender, System::EventArgs^ e) { // [Programmer-defined method WITHOUT a return type] //double amount; //Double::TryParse(txtAmount->Text, amount); //ConvertIt(amount); // [Programmer-defined method WITH a return type] //double amount,answer; //Double::TryParse(txtAmount->Text, amount); //answer = ConvertIt(amount); //txtAnswer->Text = answer.ToString();
I'm not understanding the 3rd method which is to use a method with a reference parameter to return
the value:
The book has it set up like this:
private: System::Void btnConvert_Click(System::Object^ sender, System::EventArgs^ e) { // [Programmer-defined method with a reference parameterto return the value]
Previously, I was just passing amount to the method where [answer] could be calculated. I don't understand why answer is being passed when it hasn't been calculated?
double amount, answer; Double::TryParse(txtAmount->Text, amount); ConvertIt(amount,answer); txtAnswer->Text = answer.ToString();Saturday, October 31, 2009 5:35 AM
Answers
-
The 2nd parameter to double::TryParse isn't really a reference parameter, it's an out parameter. This has similar semantics to a reference parameter, but different expectations on usage. Such a thing isn't formalized in C++, only in C++/CLI (and even then, not to the extent that it is in C#).
- Proposed as answer by Geert van Horrik Saturday, October 31, 2009 4:30 PM
- Marked as answer by oldyeller Sunday, November 1, 2009 2:30 AM
Saturday, October 31, 2009 8:42 AM
All replies
-
The 2nd parameter to double::TryParse isn't really a reference parameter, it's an out parameter. This has similar semantics to a reference parameter, but different expectations on usage. Such a thing isn't formalized in C++, only in C++/CLI (and even then, not to the extent that it is in C#).
- Proposed as answer by Geert van Horrik Saturday, October 31, 2009 4:30 PM
- Marked as answer by oldyeller Sunday, November 1, 2009 2:30 AM
Saturday, October 31, 2009 8:42 AM -
Sorry, I'm not understanding. I'm only parsing and then passing the amount since the answer depends on the amount to be calculated.
When I used the programmer method without a return type. I was just passing the amount. Like the code below:
private: System::Void btnConvert_Click(System::Object^ sender, System::EventArgs^ e) { // [Programmer-defined method WITHOUT a return type] //double amount; //Double::TryParse(txtAmount->Text, amount); //ConvertIt(amount);
I was then passing amount to the function ConvertIt, which is below:
private: void ConvertIt(double dis_amount) //{ // [Programmer-defined method WITHOUT a return type] //private: void ConvertIt(double dis_amount) //{ //double answer; //if (cboFrom->Text == "millimeter" && cboTo->Text == "inch") // answer = dis_amount / 25.4;
I don't really understand setting up the function with this reference parameter to return a value when answer is one of the parameters being passed. I can understand it being passed to a display or output function but I don't understand how they are using it here. I know this must be a good logical reason for setting it up the way they are asking but I don't understand it.Saturday, October 31, 2009 5:04 PM -
I don't really understand setting up the function with this reference parameter to return a value when answer is one of the parameters being passed. I can understand it being passed to a display or output function but I don't understand how they are using it here. I know this must be a good logical reason for setting it up the way they are asking but I don't understand it.
I have no clue what your question is. answer isn't being passed anywhere in the code you've shown in this post.Saturday, October 31, 2009 5:57 PM