call by reference
-
hello, i want to do the same thing using ""call by refernce method""....can anybody alter the code andf show to me the differnce please.... here i am using call by value i want to do this using call byt reference.....please the codes but the functionality should be the same so that it will be easy to understand,,,,thanks class Program { public static int Add(int x, int y) { int result = a + b; Console.WriteLine("{0}", result); Console.ReadLine(); return 10; } static void Main(string[] args) { int sum = Add(10, 20); Console.WriteLine("{0}", sum); Console.ReadLine(); } } }
C#
-
hello, i want to do the same thing using ""call by refernce method""....can anybody alter the code andf show to me the differnce please.... here i am using call by value i want to do this using call byt reference.....please the codes but the functionality should be the same so that it will be easy to understand,,,,thanks class Program { public static int Add(int x, int y) { int result = a + b; Console.WriteLine("{0}", result); Console.ReadLine(); return 10; } static void Main(string[] args) { int sum = Add(10, 20); Console.WriteLine("{0}", sum); Console.ReadLine(); } } }
C#
How about
public static int Add(object x,object y)
{
int First;
int Second;
try
{
First = (int)x;
Second = (int)y;
return First + Second;
}
catch(Exception ex)
{
throw new Exception("Invalid values");
}
return 0;
}
-
How about
public static int Add(object x,object y)
{
int First;
int Second;
try
{
First = (int)x;
Second = (int)y;
return First + Second;
}
catch(Exception ex)
{
throw new Exception("Invalid values");
}
return 0;
}
hi, thanks for trying to help.... but navneeth can you alter the previous program that will help me understand better....since i am not familiar with try catch and finally methods.....ie exceptions..... i will be glad if you can alter the code and show to me that i have written... thanks agin and expecting a reply
C#
-
hello, i want to do the same thing using ""call by refernce method""....can anybody alter the code andf show to me the differnce please.... here i am using call by value i want to do this using call byt reference.....please the codes but the functionality should be the same so that it will be easy to understand,,,,thanks class Program { public static int Add(int x, int y) { int result = a + b; Console.WriteLine("{0}", result); Console.ReadLine(); return 10; } static void Main(string[] args) { int sum = Add(10, 20); Console.WriteLine("{0}", sum); Console.ReadLine(); } } }
C#
class Program
{
public static int Add(ref int x, ref int y)
{
int result = x + y;
Console.WriteLine("{0}", result);
Console.ReadLine();
return 10;
}
static void Main(string[] args)
{
int FirstNo = 10;
int SecondNo = 20;
int sum = Add(ref FirstNo ,ref SecondNo);
Console.WriteLine("{0}", sum);
Console.ReadLine();
}
}Hope this helps -- modified at 2:56 Friday 19th October, 2007
-
class Program
{
public static int Add(ref int x, ref int y)
{
int result = x + y;
Console.WriteLine("{0}", result);
Console.ReadLine();
return 10;
}
static void Main(string[] args)
{
int FirstNo = 10;
int SecondNo = 20;
int sum = Add(ref FirstNo ,ref SecondNo);
Console.WriteLine("{0}", sum);
Console.ReadLine();
}
}Hope this helps -- modified at 2:56 Friday 19th October, 2007
hi, thanks a lot Navneeth ......your message realy helped.....i have altered the code a bit ......can you check and tell me whether my synatax is correct or not. i tried to run it and its running fine ....but i need your comment also....in the main function i made a slight alteration from yours.....plz check that and inform me class Program { public static int Add(ref int x, ref int y) { int a = x; //saying that henceforth a will represent x int b y ; //saying that henceforth b will represent y int result = a + b; Console.WriteLine("{0}", result); return 10; } static void Main(string[] args) { int a = 10; //passing values to a and b int b = 20; int sum = Add(ref a,ref b); Console.WriteLine("{0}", sum); Console.ReadLine(); } } }
C#
-
hi, thanks a lot Navneeth ......your message realy helped.....i have altered the code a bit ......can you check and tell me whether my synatax is correct or not. i tried to run it and its running fine ....but i need your comment also....in the main function i made a slight alteration from yours.....plz check that and inform me class Program { public static int Add(ref int x, ref int y) { int a = x; //saying that henceforth a will represent x int b y ; //saying that henceforth b will represent y int result = a + b; Console.WriteLine("{0}", result); return 10; } static void Main(string[] args) { int a = 10; //passing values to a and b int b = 20; int sum = Add(ref a,ref b); Console.WriteLine("{0}", sum); Console.ReadLine(); } } }
C#
kabutar wrote:
return 10;
Why this return ? You have to return the result here. Remaining all looks fine. Hope you are cleared with call by reference and value concepts and how it works.
-
kabutar wrote:
return 10;
Why this return ? You have to return the result here. Remaining all looks fine. Hope you are cleared with call by reference and value concepts and how it works.
hi, Navneeth what i was trying to do here is that i need the value of 10 to be returned... ie we can say return a; the value assigned to a is 10... so that value of a =10 will be returned ans stored inside sum so that we can use it for some other operation..... am i correct Navneeth......?? also one more doubt in your code u have used a,b and first number and second number .what is the purpose of those..... ie we have declred x and y as ref variables and are given refrence to a andf b..... so what is the use of using firstnumber and secondnumber..... please explain ..... thanks again for comming to my help
C#
-
hi, Navneeth what i was trying to do here is that i need the value of 10 to be returned... ie we can say return a; the value assigned to a is 10... so that value of a =10 will be returned ans stored inside sum so that we can use it for some other operation..... am i correct Navneeth......?? also one more doubt in your code u have used a,b and first number and second number .what is the purpose of those..... ie we have declred x and y as ref variables and are given refrence to a andf b..... so what is the use of using firstnumber and secondnumber..... please explain ..... thanks again for comming to my help
C#
kabutar wrote:
also one more doubt in your code u have used a,b and first number and second number .what is the purpose of those.
It was a typing mistake. corrected now.
-
kabutar wrote:
also one more doubt in your code u have used a,b and first number and second number .what is the purpose of those.
It was a typing mistake. corrected now.
Thanks a lot ..........:) cheers to you i understood something that i always had difficulty in understanding.... :) :)
-
kabutar wrote:
also one more doubt in your code u have used a,b and first number and second number .what is the purpose of those.
It was a typing mistake. corrected now.
-
hello, i want to do the same thing using ""call by refernce method""....can anybody alter the code andf show to me the differnce please.... here i am using call by value i want to do this using call byt reference.....please the codes but the functionality should be the same so that it will be easy to understand,,,,thanks class Program { public static int Add(int x, int y) { int result = a + b; Console.WriteLine("{0}", result); Console.ReadLine(); return 10; } static void Main(string[] args) { int sum = Add(10, 20); Console.WriteLine("{0}", sum); Console.ReadLine(); } } }
C#
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 valuex
andy
(by value so they can't change) to populateresult
, 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."
-
Thanks a lot ..........:) cheers to you i understood something that i always had difficulty in understanding.... :) :)
-
How about
public static int Add(object x,object y)
{
int First;
int Second;
try
{
First = (int)x;
Second = (int)y;
return First + Second;
}
catch(Exception ex)
{
throw new Exception("Invalid values");
}
return 0;
}
That's not sending argument by reference, that's sending references as arguments.
--- "Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things." -- Douglas Adams
-
That's not sending argument by reference, that's sending references as arguments.
--- "Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things." -- Douglas Adams
Guffa wrote:
hat's not sending argument by reference, that's sending references as arguments.
Yes that was a mistake. I corrected that in second post
-
Guffa wrote:
hat's not sending argument by reference, that's sending references as arguments.
Yes that was a mistake. I corrected that in second post
Why didn't you just edit the post?
--- "Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things." -- Douglas Adams