Jump to content

C# Constructor & Object Confusion.

kaddle

i am mind shatteringly confused right now, and i have been for the past 24 hours. in the book that i am using to learn C# it ran me through an exercise to explain classes, properties, constructors, objects, etc. i have a basic beginner understanding of it for the most part. my confusion is in how the code is printed out by the compiler. i'm going to copy paste the entire program because i don't know how much of it is necessary for this thread. some of you might remember that i'm very new to learning C#, so i might make mistakes in my explanations and questions. i read a lot of the pages in the book for this exercise a few times over, but i still can't solve this issue. either the information isn't in the book or i kept missing it while reading and i'm right in being confused, or i am just a complete and utter idiot.

 

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

namespace Oop
{
    class Staff
    {
        private string nameOfStaff;
        private const int hourlyRate = 30;
        private int hWorked;
        public int HoursWorked
        {
            get
            {
                return hWorked;
            }
            set
            {
                if (value > 0)
                    hWorked = value;
                else
                    hWorked = 0;
            }
        }
        public void PrintMessage()
        {
            Console.WriteLine("Calculating Pay…");
        }
        public int CalculatePay()
        {
            PrintMessage();
            int staffPay;
            staffPay = hWorked * hourlyRate;
            if (hWorked > 0)
                return staffPay;
            else
                return 0;
        }
        public int CalculatePay(int bonus, int allowance)
        {
            PrintMessage();
            if (hWorked > 0)
                return hWorked * hourlyRate + bonus + allowance;
            else
                return 0;
        }
        public override string ToString()
        {
            {
                return "Name of Staff =" + nameOfStaff + ", hourlyRate = " + hourlyRate + ", hWorked = " + hWorked;
            }
        }
        public Staff(string name)
        {
            nameOfStaff = name;
            Console.WriteLine("\n" + nameOfStaff);
            Console.WriteLine("--------------------");
        }
        public Staff(string firstName, string lastName)
        {
            nameOfStaff = firstName + " " + lastName;
            Console.WriteLine("\n" + nameOfStaff);
            Console.WriteLine("--------------------");
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            int pay;
            Staff staff1 = new Staff("Peter");
            staff1.HoursWorked = 160;
            pay = staff1.CalculatePay(1000, 400);
            Console.WriteLine("Pay = {0}", pay);
            Staff staff2 = new Staff("Jane", "Lee");
            staff2.HoursWorked = 160;
            pay = staff2.CalculatePay();
            Console.WriteLine("Pay = {0}", pay);
            Staff staff3 = new Staff("Carol");
            staff3.HoursWorked = -10;
            pay = staff3.CalculatePay();
            Console.WriteLine("Pay = {0}", pay);
            Console.ReadLine();
        }
    }
}

 

when the code runs, what gets printed out is:

 

9087iyjufthn.png

 

Spoiler

so of course the program is a payment calculator. in the Main() method i made the three objects which each represent an employee. after the objects are instantiated, the HoursWorked property is accessed to change the value of the private field. then the pay variable is assigned the result of the CalculatePay method, which also takes in the values of bonus and allowance into the parenthesis as arguments in this case, before returning the value, but it also runs the PrintMessage() method before assigning the value (i'm probably wrong about that. it's very confusing). so then the WriteLine() method prints the value of pay, which i guess also prints out the PrintMessage() method? but what is even more confusing is that somehow it printed out the string that is passed in the parenthesis of the object, which are the employee names; and it also prints out the "--------------------" under the employee name.

 

i am feeling quite stupid at the moment. in the Main() method the only code in the WriteLine() method is |"Pay = {0}", pay|.

how exactly did the names of the employees, the "--------------------", and the "Calculating Pay." get printed out as well, and all in perfect order?

Link to comment
Share on other sites

Link to post
Share on other sites

When the CalculatePay() method (Class Function) is called, which then calls the PrintMessage() method, which is what prints "Calculating Pay". The part that prints "Name" and "------------" is found in the constructor. When staffx = new Staff("name") is called, that's when the constructor is called and then it prints the name.

Current Desktop Build | 2200G | RX 580 4GB | 8GB RAM | CTRL | Logitech G Pro Wireless

Laptop | 2018 MBA 256/16GB | MX Master 

Link to comment
Share on other sites

Link to post
Share on other sites

Basically what zlol said.

 

If you want to see how it works yourself, put a breakpoint (red debug bubble) on start of Main() method. Start a program and go trough it step by step.
Then use F10 to move line by line. And F11 to go into a method.

Laptop: Acer V3-772G  CPU: i5 4200M GPU: GT 750M SSD: Crucial MX100 256GB
DesktopCPU: R7 1700x GPU: RTX 2080 SSDSamsung 860 Evo 1TB 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×