Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8651697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:08:03+00:00 2026-06-12T14:08:03+00:00

I’ve got a program snippet here that allows the creation of an Employee object

  • 0

I’ve got a program snippet here that allows the creation of an Employee object with simple properties of age, id, name and pay. Just playing around with it I noticed that

 Console.WriteLine(joe.Age+1); is my Main() method returns one, 

but Console.WriteLine(joe.Age++); returns 0. I know that the Age property, per the constructors is going to be initialized to 0, but why isn’t 1 being added with the ++ operator? EDIT: I found the source of the strange behavior. In the Age property I have empAge=Age when it should’ve been equal to value

source:

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

namespace EmployeeApp
{
    class Employee
    {
        //field data
        //notice the the fields are declared as private
        //these fields are used in the constructors
        private string empName;
        private int empID;
        private float currPay;
        private int empAge;

        //properties! private field data should be accessed via public properties
        //note that properties don't use parentheses ()
        //within the set scope you see the 'value' contextual keyword
        //it represents the value being assigned by the caller and it will always be the same 
        //underlying data type as the property itself
        public int Age
        {
            get { return empAge; }
            set { empAge = Age; }
        }


        public string Name
        {
            get { return empName; }
            set
            {
                if (value.Length > 15)
                    Console.WriteLine("this name is too long.");
                else
                    empName = value;
            }
        }
        public int ID
        {
            get { return empID; }
            set { empID = value; }
        }
        public float pay
        {
            get { return currPay; }
            set { currPay = value; }
        }

        //constructors
        public Employee() { }

        public Employee(string name, int id, float pay, int age)
        {
            empName = name;
            empID = id;
            currPay = pay;
            empAge = age;
        }

        //methods
        //the int parameter that this method takes will come from somewhere in the Main method
        //currpay is a private field
        public void GiveBonus(float amount)
        {
            currPay += amount;
        }
        public void DisplayStats()
        {
            Console.WriteLine("name: {0}", empName);
            Console.WriteLine("ID: {0}", empID);
            Console.WriteLine("pay: {0}", currPay);
            Console.WriteLine("age: {0}", empAge);
        }
    }

}

Main method here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//Encapsulation using traditional accessors/mutators or get/set methods
//the role of a get method is to return to the caller the current value of the underlying state data
//a set method allows the caller ot change the current value of the state data

//you need to have a getter and a setter for every field that the class has

namespace EmployeeApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("fun with encapsulation");
            //Employee emp = new Employee("marvin", 456, 4000, 56);
            //emp.GiveBonus(3);
           // emp.DisplayStats();
           // emp.Name = "wilson";
           // emp.DisplayStats();

            Employee joe = new Employee();

            Console.WriteLine(joe.Age++);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:08:04+00:00Added an answer on June 12, 2026 at 2:08 pm

    In your Age property, you are not changing the empAge member to the value passed in. This is probably why you aren’t seeing any changes when you tried ++ multiple times.

    public int Age 
    { 
        get { return empAge; } 
        set { empAge = Age;  } // this does not set the value!
    } 
    

    Use the value instead:

    public int Age 
    { 
        get { return empAge;   } 
        set { empAge = value;  } // use the value passed in
    } 
    

    And as others have pointed out, you are using the postfix version of the ++ operator. The prefix version will increment the amount first before writing the property to the console.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.