Pass value by reference

image_pdfimage_print

   

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/
 using System;

 namespace PassByRef
 {

     public class Time3
     {
         // private member variables
         private int Year;
         private int Month;
         private int Date;
         private int Hour;
         private int Minute;
         private int Second;

         // Property (read only)
         public int GetHour()
         {
             return Hour;
         }

         // public accessor methods
         public void DisplayCurrentTime()
         {
             System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}",
                 Month, Date, Year, Hour, Minute, Second);
         }

         public void GetTime(int h, int m, int s)
         {
             h = Hour;
             m = Minute;
             s = Second;
         }

         // constructor
         public Time3(System.DateTime dt)
         {

             Year = dt.Year;
             Month = dt.Month;
             Date = dt.Day;
             Hour = dt.Hour;
             Minute = dt.Minute;
             Second = dt.Second;
         }


     }

    public class PassByRefTester
    {
       public void Run()
       {
           System.DateTime currentTime = System.DateTime.Now;
           Time3 t = new Time3(currentTime);
           t.DisplayCurrentTime();

           int theHour = 0;
           int theMinute = 0;
           int theSecond = 0;
           t.GetTime(theHour, theMinute, theSecond);
           System.Console.WriteLine("Current time: {0}:{1}:{2}",
               theHour, theMinute, theSecond);

       }

       static void Main()
       {
          PassByRefTester t = new PassByRefTester();
          t.Run();
       }
    }
 }