Tuesday, May 27, 2014

When do we use reference parameter?

Sometimes you need to get more than one piece of data from a function, for example imagine a function called GetNewColorAndText...


You can return the color from the function or you can return the text, but not easily both. This illustrates:



public static string GetNewColorAndText(ref Color Col, string sText)
{
Col = Color.FromArgb (255,Col.R/2,Col.G/2,Col.B/2)
string sRet = sText + "and darker" ;
return sRet ;
}

public static void Test()
{
Color MyCol = Color.FromArgb(255, 45, 67, 89);
string sDesc = "a color";

string sNewDesc = GetNewColorAndText(ref MyCol, sDesc);

// Now MyCol will have a different value from before
// the call
}



MyCol is changed by the call to GetNewColorAndText.




http://www.ransen.com Cad and Graphics software


No comments:

Post a Comment