I just started c# and I am having trouble with the (get; set;) the program I am trying to work on is employee class that has two objects of two employee's with both having a string first name and string last name. With a monthly wage for both employee's could someone help me out. The names of employee's the user has to enter them.

using System;

  public class Employee
    {
        private string name;
        private double wage;

      public string Name
        {
            get { return name; }
            set { name = value; }
        }

      public double Wage
      {
          get { return wage; }
          set { if(value<-0.0)
                wage = value; }

      }

  }


  using System;

public class TestEmployee
{
    static void Main()
    {
        Employee e1 = new Employee();
        e1.Name = "Michael";

        Console.WriteLine("Employee first name: {0}", e1.Name);

        Employee e2 = new Employee();
        e2.Name = "Gutierrez";

        Console.WriteLine("Employee last name: {0}", e2.Name);

        Employee w1 = new Employee();
        w1.Wage = 3500.00;

        Console.WriteLine("Employee one monthly salary is: {0}", w1.Wage);

        double ywage = 12 * 3500;

        Console.WriteLine("Employee one Year salary is: {0}", ywage);
        Console.WriteLine("\n\n\n");

        Employee e3 = new Employee();
        e3.Name = "Marissa";

        Console.WriteLine("Employee two first name: {0}", e3.Name);

        Employee e4 = new Employee();
        e4.Name = "Gallegos";

        Console.WriteLine("Employee last name: {0}", e4.Name);

        Employee w2 = new Employee();
        w2.Wage = 4000.00;

        Console.WriteLine("Employee two monthly salary is: {0}", w2.Wage);

        double ywage1 = 12 * 4000;

        Console.WriteLine("Employee two Year salary is: {0}", ywage1);
    }
}

I am not sure if I am doing it correct but that is what I have so far. Here are the instructions:Create a class called Employee that includes three pieces of information as instance variables — a first name (type string), a last name (type string) and a monthly salary (decimal). Your class should have a constructor that initializes the three values. Provide a property with a get and set accessor for any instance variables. If the monthly salary is negative, the set accessor should leave the instance variable unchanged. Write a test application named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary. Then give each Employee a 10% raise and display each Employee’s yearly salary again.

Your class doesn't currently contain what is asked for, nor contain a constructor, they are looking for the following:

public class Employee
{
    private string firstName;
    private string lastName;
    private double wage;

    public Employee (string FName, string LName, double WageGiven) //The constructor
    {
        FirstName = FName;
        LastName = LName;
        Wage = WageGiven;
    }

    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

    public string LastName
    {
        get { return lastName; }
        set { lastName = value; }
    }

    public double Wage
    {
        get { return wage; }
        set { if(value >= 0.0) { wage = value; } } //Your value checking was wrong here
    }
}

Which you can then implement in the following way:

//Create the employees using the new constructors we have
Employee EmpOne = new Employee("Joe", "Bloggs", 1000);
Employee EmpTwo = new Employee("Jimi", "Hendrix", 2000);
Employee EmpThree = new Employee("Calvin", "Harris", -100); //Nothing should happen in terms of wage for this guy

//Show Wages
Console.WriteLine("Wages: EmployeeOne-£{0}, EmployeeTwo-£{1}, EmployeeThree-£{2}", EmpOne.Wage, EmpTwo.Wage, EmpThree.Wage);

//Increase Wages
EmpOne.Wage = EmpOne.Wage * 1.10;
EmpTwo.Wage = EmpTwo.Wage * 1.10;
EmpThree.Wage = EmpThree.Wage * 1.10;

//Show New Wages
Console.WriteLine("Wages: EmployeeOne-£{0}, EmployeeTwo-£{1}, EmployeeThree-£{2}", EmpOne.Wage, EmpTwo.Wage, EmpThree.Wage);

Hope that helps :)

Edited 4 Years Ago by Mike Askew

This article has been dead for over six months. Start a new discussion instead.