Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. call by reference

call by reference

Scheduled Pinned Locked Moved C#
csharp
15 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kabutar
    wrote on last edited by
    #1

    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#

    N M 3 Replies Last reply
    0
    • K kabutar

      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#

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      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;
      }


      My Website | Ask smart questions

      K G 2 Replies Last reply
      0
      • N N a v a n e e t h

        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;
        }


        My Website | Ask smart questions

        K Offline
        K Offline
        kabutar
        wrote on last edited by
        #3

        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#

        1 Reply Last reply
        0
        • K kabutar

          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#

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          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


          My Website | Ask smart questions

          K 1 Reply Last reply
          0
          • N N a v a n e e t h

            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


            My Website | Ask smart questions

            K Offline
            K Offline
            kabutar
            wrote on last edited by
            #5

            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#

            N 1 Reply Last reply
            0
            • K kabutar

              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#

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              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.


              My Website | Ask smart questions

              K 1 Reply Last reply
              0
              • N N a v a n e e t h

                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.


                My Website | Ask smart questions

                K Offline
                K Offline
                kabutar
                wrote on last edited by
                #7

                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#

                N 1 Reply Last reply
                0
                • K kabutar

                  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#

                  N Offline
                  N Offline
                  N a v a n e e t h
                  wrote on last edited by
                  #8

                  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.


                  My Website | Ask smart questions

                  T K 2 Replies Last reply
                  0
                  • N N a v a n e e t h

                    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.


                    My Website | Ask smart questions

                    T Offline
                    T Offline
                    Trustapple
                    wrote on last edited by
                    #9

                    Thanks a lot ..........:) cheers to you i understood something that i always had difficulty in understanding.... :) :)

                    N 1 Reply Last reply
                    0
                    • N N a v a n e e t h

                      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.


                      My Website | Ask smart questions

                      K Offline
                      K Offline
                      kabutar
                      wrote on last edited by
                      #10

                      Awesome work.....Navneeth..... thanks a lot for helping me out.... thanks a million again

                      C#

                      1 Reply Last reply
                      0
                      • K kabutar

                        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#

                        M Offline
                        M Offline
                        Malcolm Smart
                        wrote on last edited by
                        #11

                        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."

                        1 Reply Last reply
                        0
                        • T Trustapple

                          Thanks a lot ..........:) cheers to you i understood something that i always had difficulty in understanding.... :) :)

                          N Offline
                          N Offline
                          N a v a n e e t h
                          wrote on last edited by
                          #12

                          Welome. :cool:


                          My Website | Ask smart questions

                          1 Reply Last reply
                          0
                          • N N a v a n e e t h

                            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;
                            }


                            My Website | Ask smart questions

                            G Offline
                            G Offline
                            Guffa
                            wrote on last edited by
                            #13

                            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

                            N 1 Reply Last reply
                            0
                            • G Guffa

                              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

                              N Offline
                              N Offline
                              N a v a n e e t h
                              wrote on last edited by
                              #14

                              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


                              My Website | Ask smart questions

                              G 1 Reply Last reply
                              0
                              • N N a v a n e e t h

                                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


                                My Website | Ask smart questions

                                G Offline
                                G Offline
                                Guffa
                                wrote on last edited by
                                #15

                                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

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups