Wrox Programmer Forums
|
C# 4.0 aka C# 2010 General Discussion Discussions about the C# 4.0, C# 4, Visual C# 2010 language and tool not related to any specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 4.0 aka C# 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 14th, 2011, 09:57 AM
Registered User
 
Join Date: Nov 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Novice in C# -Incrementing Employee Class

Hello,

I have written a code that needs to increment my employee object by 1 for each instance of the class Employee.I am using a variable private static int numEmployee = 0;

My program runs fine, but it keeps giving me total employees = "2" and "3" for employee1 and employee2, and I am not sure what I am doing wrong. Please look at this code and offer any help !!!

This is my first post, so not sure if this format is correct or not. I used Visual Studio 2010 for C# to compile.

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

namespace Help

{
class Employee
{
private string firstName;
private string lastName;
private char gender;
private int dependents;
private double annualSalary;
private double pay;
public static int numEmployees = 0; // created static variable

public const string DEFAULT_FIRST = " not given";
public const string DEFAULT_LAST = " not given";
public const char DEFAULT_GENDER = 'U';
public const int DEFAULT_DEPENDENTS = 0;
private const double DEFAULT_ANNUALSALARY = 20000;


public Employee()
{
FirstName = DEFAULT_FIRST;
LastName = DEFAULT_LAST;
Gender = DEFAULT_GENDER;
Dependents = DEFAULT_DEPENDENTS;
AnnualSalary = DEFAULT_ANNUALSALARY;
numEmployees++; // incremented numEmployees

}

public Employee(string first, string last, char gen, int dep, double salary)
{

DisplayEmployeeInformation(first, last, gen, dep, salary);
numEmployees++; //incremented numEmployees

}

~Employee()
{
Console.WriteLine("Cancel the employee!!");
}

public string FirstName
{
get
{ return firstName; }
set
{firstName = value;}
}
public string LastName
{
get
{ return lastName; }
set
{lastName = value;}
}
public char Gender
{
get
{ return gender; }
set
{gender = value;}

}
public int Dependents
{
get
{ return dependents; }
set
{ dependents = value;}
}

public double AnnualSalary
{
get { return annualSalary; }
set
{
if(value > 19999)
annualSalary = value;
else
annualSalary = DEFAULT_ANNUALSALARY;
}
}
public static int GetNumEmployees() //
{
return numEmployees;
}
public double CalculatePay()
{
return (annualSalary / 52);

}
public override string ToString()
{
string str;

str = "Employee AnnualSalary: $" + AnnualSalary.ToString("N2") + "\n ";
return str;
}
public void DisplayEmployeeInformation(string first, string last, char gen, int dep, double salary)
{

Console.WriteLine("First Name: " + first);
Console.WriteLine("Last Name: " + last);
Console.WriteLine("Gender: " + gen);
Console.WriteLine("Dependents: " + dep);
Console.WriteLine("Annual Salary:$ " + salary.ToString("N2"));
annualSalary = salary;
pay = CalculatePay();
Console.WriteLine("Employee Weekly Pay:$ " + pay.ToString("N2"));
Console.WriteLine("Total Employees:" + numEmployees);
}

}

}

***** start of Program.cs *****

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

namespace Help
{
public class Program
{
static void Main(string[] args)
{

DisplayApplicationInformation();
DisplayDivider("Start Program");

//Employee.DisplayEmployeeInformation();
DisplayDivider("Employee Information");

DisplayDivider("Creating and testing employee1");
Employee employee1 = new Employee();
Employee.GetNumEmployees(); // is this where we access the
employee1.FirstName = GetInput("First Name");
employee1.LastName = GetInput("Last Name");
employee1.Gender = char.Parse(GetInput("Gender"));
employee1.Dependents = int.Parse(GetInput("Dependents"));
employee1.AnnualSalary = double.Parse(GetInput("Salary"));



DisplayDivider("Creating and testing employee2");
Employee employee2 = new Employee();
Employee.GetNumEmployees();
employee2.FirstName = GetInput("First Name");
employee2.LastName = GetInput("Last Name");
employee2.Gender = char.Parse(GetInput("Gender"));
employee2.Dependents = int.Parse(GetInput("Dependents"));
employee2.AnnualSalary = double.Parse(GetInput("Salary"));


Console.Clear();
Employee employee1write = new Employee(employee1.FirstName, employee1.LastName, employee1.Gender, employee1.Dependents, employee1.AnnualSalary);
Console.WriteLine();
Employee employee2write = new Employee(employee2.FirstName, employee2.LastName, employee2.Gender, employee2.Dependents, employee2.AnnualSalary);


TerminateApplication();

}
private static void DisplayApplicationInformation()
{
Console.WriteLine("Welcome to Employee Class Program");
Console.WriteLine("CIS247, Week 3 Lab");
Console.WriteLine("Name: Chuck");
Console.WriteLine("This program will increment the employee class to count the employees");
Console.WriteLine();
}

private static void DisplayDivider(string outputTitle)
{
Console.WriteLine("***************" + outputTitle + " " + "***************");
}

private static string GetInput(string inputType)
{
string strInput = String.Empty;
Console.Write(" Please enter " + inputType + " : ");
strInput = Console.ReadLine();

return strInput;
}

private static void TerminateApplication()
{
DisplayDivider("Program Termination");
Console.Write("Thank You! Please press any key to terminate the program");
Console.ReadKey();
}
}
}
 
Old December 15th, 2011, 05:15 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Can you try onething:

Place a bookamrk in the increment statement and check if the method is called more that once per object initalization

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old May 1st, 2014, 01:38 AM
Friend of Wrox
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

You are not getting numEmployees=2 and 3 from displaying employee1 and employee2 counts after construction. You are getting numEmployee=2 and 3 from employee1write and employee2write. When you construct the "write" version of your employee class the first thing you do is display their information then you increment. So on the creation of the 3rd employee you are displaying the count from the 2nd construction, and on the creation of the 4th you are display the count from the 3rd. No where in your code do you display the counts from the first 2 constructions.

Last edited by mmorgan30; May 1st, 2014 at 01:45 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
employee leave update... vamshidhar ASP.NET 3.5 Professionals 0 July 12th, 2011 02:52 PM
Employee attendance form with DataGridView Dot_Net_Dev C# 3 June 8th, 2011 02:25 AM
Select the top 3 record of each employee phungleon Access 1 June 16th, 2007 01:16 AM
Select employee name spelling mateenmohd SQL Server 2000 21 June 7th, 2004 02:23 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.