O'Reilly Confused me on classes
-
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.
-
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.
Herboren wrote:
the values aren't actively stored in my variables yet
Yes, they are, why do you think otherwise?
int
s are value types. -
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.
Time t = new Time(currentTime);
The above code calls the constructor that takes a
System.DateTime
as input, and copies the values from theDateTime
into the new object's variables. So you now have aTime
object containing values that correspond to thecurrentTime
object. The next line calls theDisplayCurrentTime()
method on theTime
objectt
, which displays the values previously stored. Does that make sense?The best things in life are not things.
-
Time t = new Time(currentTime);
The above code calls the constructor that takes a
System.DateTime
as input, and copies the values from theDateTime
into the new object's variables. So you now have aTime
object containing values that correspond to thecurrentTime
object. The next line calls theDisplayCurrentTime()
method on theTime
objectt
, which displays the values previously stored. Does that make sense?The best things in life are not things.
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.
-
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.
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 theTime
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.