What does it mean when the function look like this?
bool Connect([ref string errormessage])
{
\\Code
}
Do I call it like this?
string error = "";
if(!<MyInstance>.Connect(error))
MessageBox.Show(error);
What does it mean when the function look like this?
bool Connect([ref string errormessage])
{
\\Code
}
Do I call it like this?
string error = "";
if(!<MyInstance>.Connect(error))
MessageBox.Show(error);
Assuming the function call is as below, since the above has a syntax error.
bool Connect(ref string errormessage)
{
\\Code
}
Then, this means that
The argument errormessage is being passed as reference, not value.
When an argument is passed as reference:
string error = ""; //Point 1
if(!<MyInstance>.Connect(ref error)) //Point 2
bool Connect(ref string errormessage) //Point 2
{
errormessage = "Error Occurred";
// At this moment the value of error becomes 'Error Occurred' since
// it was passed by reference - Point 3
}
Regarding the syntax error, [ref string errormessage]
will give a syntax error, since it's not a valid attribute i.e., [Optional] string errormessage
.
Additionally, using the optional attribute with a ref
is not of much use, as an argument passed with ref cannot have a default value.
Source: MSDN.