Click here to Skip to main content
15,917,862 members
Home / Discussions / C#
   

C#

 
GeneralRe: call by reference Pin
kabutar18-Oct-07 20:27
kabutar18-Oct-07 20:27 
GeneralRe: call by reference Pin
N a v a n e e t h18-Oct-07 20:34
N a v a n e e t h18-Oct-07 20:34 
GeneralRe: call by reference Pin
kabutar18-Oct-07 20:47
kabutar18-Oct-07 20:47 
GeneralRe: call by reference Pin
N a v a n e e t h18-Oct-07 20:57
N a v a n e e t h18-Oct-07 20:57 
GeneralRe: call by reference Pin
Trustapple18-Oct-07 21:03
Trustapple18-Oct-07 21:03 
GeneralRe: call by reference Pin
N a v a n e e t h18-Oct-07 21:22
N a v a n e e t h18-Oct-07 21:22 
GeneralRe: call by reference Pin
kabutar18-Oct-07 21:06
kabutar18-Oct-07 21:06 
AnswerRe: call by reference Pin
Malcolm Smart18-Oct-07 21:12
Malcolm Smart18-Oct-07 21:12 
You call by reference so you can change the parameters within the call. You sample has no benefit using references. A better example would be

public static void Add(int x, int y , ref int result)
{
   result = x + y;
}

static void Main(string[] args)
{
   int x = 5;
   int y = 5;
   int result = 0;
   Add(x , y , ref result);
   Console.WriteLine("{0}", result);
   Console.ReadLine();
}


Here, the Add method has no internal members. It uses the pass by value x and y (by value so they can't change) to populate result, passed by reference so it can change. Within the Add signature, the parameters can be called different names....

public static void Add(int firstValue , int secondValue , ref int firstValueAddedToSecondValue)
{
   firstValueAddedToSecondValue = firstValue + secondValue;
}

static void Main(string[] args)
{
   int x = 5;
   int y = 5;
   int result = 0;
   Add(x , y , ref result);
   Console.WriteLine("{0}", result);
   Console.ReadLine();
}


The advantage of this method, is you can then use a return value for success or failure of your Add function (if it becomes more complex)

public static bool Add(int x, int y, ref int result)
{
   try
   {
      result = ReallyComplexCalculationDoneBy3rdPartyLib( x , y );
   }
   catch( Exception e )
   {
      //handle exception - this is for example only
      return false;
   }

   return true;
}

static void Main(string[] args)
{
   int x = 5;
   int y = 5;
   int result = 0;

   bool AddWorked = Add(x , y , ref result);

   if (AddWorked)
      Console.WriteLine("{0}", result);
   else
      Console.WriteLine("Addition failed - check log file..etc");

   Console.ReadLine();
}


Hope this helps.


"More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF


"This time yesterday, I still had 24 hours to meet the deadline I've just missed today."

QuestionHow to configure to OpenOffice through C# Pin
pashitech18-Oct-07 19:15
pashitech18-Oct-07 19:15 
QuestionHow to set entry point for an class [modified] Pin
Exelioindia18-Oct-07 19:11
Exelioindia18-Oct-07 19:11 
AnswerRe: How to set entry point for an class Pin
N a v a n e e t h18-Oct-07 19:26
N a v a n e e t h18-Oct-07 19:26 
Questionclasses inheritance Pin
kabutar18-Oct-07 18:54
kabutar18-Oct-07 18:54 
AnswerRe: classes inheritance Pin
N a v a n e e t h18-Oct-07 19:24
N a v a n e e t h18-Oct-07 19:24 
GeneralRe: classes inheritance Pin
kabutar18-Oct-07 19:38
kabutar18-Oct-07 19:38 
GeneralRe: classes inheritance Pin
N a v a n e e t h18-Oct-07 19:50
N a v a n e e t h18-Oct-07 19:50 
GeneralRe: classes inheritance Pin
Malcolm Smart18-Oct-07 21:23
Malcolm Smart18-Oct-07 21:23 
QuestionHow can I drawing over fullscreen ? Pin
Khoramdin18-Oct-07 18:53
Khoramdin18-Oct-07 18:53 
AnswerRe: How can I drawing over fullscreen ? Pin
Luc Pattyn19-Oct-07 3:18
sitebuilderLuc Pattyn19-Oct-07 3:18 
QuestionHTML PARSER problem Pin
netwearcdz118-Oct-07 17:09
netwearcdz118-Oct-07 17:09 
AnswerRe: HTML PARSER problem Pin
N a v a n e e t h18-Oct-07 18:56
N a v a n e e t h18-Oct-07 18:56 
GeneralRe: HTML PARSER problem Pin
netwearcdz119-Oct-07 9:50
netwearcdz119-Oct-07 9:50 
QuestionHow to position controls in panel dynamically? Pin
K a b u k i18-Oct-07 16:44
K a b u k i18-Oct-07 16:44 
AnswerRe: How to position controls in panel dynamically? Pin
K a b u k i18-Oct-07 19:44
K a b u k i18-Oct-07 19:44 
QuestionColumn Totals of a data grid Pin
uppuluripavan18-Oct-07 10:29
uppuluripavan18-Oct-07 10:29 
AnswerRe: Column Totals of a data grid Pin
Malcolm Smart18-Oct-07 21:44
Malcolm Smart18-Oct-07 21:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.