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. O'Reilly Confused me on classes

O'Reilly Confused me on classes

Scheduled Pinned Locked Moved C#
csharplinqquestionlearning
5 Posts 3 Posters 1 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.
  • H Offline
    H Offline
    Herboren
    wrote on last edited by
    #1

    Lets start with a piece from the book:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace TernaryOperator
    {
    class Time
    {
    // Public Member Variables
    int Year;
    int Month;
    int Date;
    int Hour;
    int Minute;
    int Second;

        // Public accessor methods
        public void DisplayCurrentTime()
        {
            Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}",
                Month, Date, Year, Hour,Minute,Second);
            Console.ReadKey();
        }
    
        // Constructor
        public Time(System.DateTime dt)
        {
            Year = dt.Year;
            Month = dt.Month;
            Date = dt.Day;
            Hour = dt.Hour;
            Minute = dt.Minute;
            Second = dt.Second;
        }
    }
    
    class Program
    {
        static void Main(string\[\] args)
        {
            System.DateTime currentTime = System.DateTime.Now;
            Time t = new Time(currentTime);
            t.DisplayCurrentTime();
        }
    
    }
    

    }

    For the constructor portion, where I declared or created the reference in the constructor, the values aren't actively stored in my variables yet, am I right?

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

    So that means my 'Time t = new Time(currentTime)' is storing the active vars into my constructor using the 'dt' reference?. Correct? I feel like I am almost lost.

    P L 2 Replies Last reply
    0
    • H Herboren

      Lets start with a piece from the book:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;

      namespace TernaryOperator
      {
      class Time
      {
      // Public Member Variables
      int Year;
      int Month;
      int Date;
      int Hour;
      int Minute;
      int Second;

          // Public accessor methods
          public void DisplayCurrentTime()
          {
              Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}",
                  Month, Date, Year, Hour,Minute,Second);
              Console.ReadKey();
          }
      
          // Constructor
          public Time(System.DateTime dt)
          {
              Year = dt.Year;
              Month = dt.Month;
              Date = dt.Day;
              Hour = dt.Hour;
              Minute = dt.Minute;
              Second = dt.Second;
          }
      }
      
      class Program
      {
          static void Main(string\[\] args)
          {
              System.DateTime currentTime = System.DateTime.Now;
              Time t = new Time(currentTime);
              t.DisplayCurrentTime();
          }
      
      }
      

      }

      For the constructor portion, where I declared or created the reference in the constructor, the values aren't actively stored in my variables yet, am I right?

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

      So that means my 'Time t = new Time(currentTime)' is storing the active vars into my constructor using the 'dt' reference?. Correct? I feel like I am almost lost.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Herboren wrote:

      the values aren't actively stored in my variables yet

      Yes, they are, why do you think otherwise? ints are value types.

      1 Reply Last reply
      0
      • H Herboren

        Lets start with a piece from the book:

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;

        namespace TernaryOperator
        {
        class Time
        {
        // Public Member Variables
        int Year;
        int Month;
        int Date;
        int Hour;
        int Minute;
        int Second;

            // Public accessor methods
            public void DisplayCurrentTime()
            {
                Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}",
                    Month, Date, Year, Hour,Minute,Second);
                Console.ReadKey();
            }
        
            // Constructor
            public Time(System.DateTime dt)
            {
                Year = dt.Year;
                Month = dt.Month;
                Date = dt.Day;
                Hour = dt.Hour;
                Minute = dt.Minute;
                Second = dt.Second;
            }
        }
        
        class Program
        {
            static void Main(string\[\] args)
            {
                System.DateTime currentTime = System.DateTime.Now;
                Time t = new Time(currentTime);
                t.DisplayCurrentTime();
            }
        
        }
        

        }

        For the constructor portion, where I declared or created the reference in the constructor, the values aren't actively stored in my variables yet, am I right?

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

        So that means my 'Time t = new Time(currentTime)' is storing the active vars into my constructor using the 'dt' reference?. Correct? I feel like I am almost lost.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3
                Time t = new Time(currentTime);
        

        The above code calls the constructor that takes a System.DateTime as input, and copies the values from the DateTime into the new object's variables. So you now have a Time object containing values that correspond to the currentTime object. The next line calls the DisplayCurrentTime() method on the Time object t, which displays the values previously stored. Does that make sense?

        The best things in life are not things.

        H 1 Reply Last reply
        0
        • L Lost User
                  Time t = new Time(currentTime);
          

          The above code calls the constructor that takes a System.DateTime as input, and copies the values from the DateTime into the new object's variables. So you now have a Time object containing values that correspond to the currentTime object. The next line calls the DisplayCurrentTime() method on the Time object t, which displays the values previously stored. Does that make sense?

          The best things in life are not things.

          H Offline
          H Offline
          Herboren
          wrote on last edited by
          #4

          Actually that made perfect sense and your put me back on track thank you. And these are all initialized explicitly, correct? And I can implicitly initialize it but changing 'int Second = 30;' as an initializer, not that I would want to but just to get the understanding of implicit and explicit.

          L 1 Reply Last reply
          0
          • H Herboren

            Actually that made perfect sense and your put me back on track thank you. And these are all initialized explicitly, correct? And I can implicitly initialize it but changing 'int Second = 30;' as an initializer, not that I would want to but just to get the understanding of implicit and explicit.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Herboren wrote:

            And these are all initialized explicitly, correct?

            Yes, statements of the form:

                    Year = dt.Year;
            

            are taking a value from the input parameter (dt) and allocating them to a variable within the current instance of the Time class; i.e. the object currently being constructed by the constructor.

            Herboren wrote:

            And I can implicitly initialize it but changing 'int Second = 30;' as an initializer, not that I would want to but just to get the understanding of implicit and explicit.

            I'm not sure I understand what you mean here: any statement of the form, variable = expression, is an explicit assignment, and can be placed anywhere, either within one of the class methods, or external to it when the access level of a variable allows. For a further discussion of explicit/implicit take a look here[^].

            The best things in life are not things.

            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