How do I test a simple class? - c#

Create an Employee class. Items to include as data members are
employee number, name, date of hire, job description, department, and
monthly salary. The class is often used to display an alphabetical
listing of all employees. Include appropriate constructors and
properties. Override the ToString ( ) method to return all data
members. Create a second class to test your Employee class.
I've created an Employee class with the proper variables, properties, and constructors, but am having trouble "testing" it through a second class. The code I have written runs without errors, but doesn't display anything (presumably the goal of the testing). Where am I going wrong in the calling section?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EmployeeProgram
{
public class EmployeeProgram
{
private int employeeNumber;
private string name;
private string hiredate;
private int monthlySalary;
private string description;
private string department;
public employee(int employeeNumber, string name, string dateOfHire, int monthlySalary, string description, string department)
{
this.employeeNumber = 456;
this.name = "Joyce";
this.hiredate = "12/15/14";
this.monthlySalary = 3200;
this.description = "Manager";
this.department = "Accounting";
}
public int EmployeeNumber
{
get
{
return employeeNumber;
}
set
{
employeeNumber = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string Hiredate
{
get
{
return hiredate;
}
set
{
hiredate = value;
}
}
public int MonthlySalary
{
get
{
return monthlySalary;
}
set
{
monthlySalary = value;
}
}
public string Department
{
get
{
return department;
}
set
{
department = value;
}
}
public string Description
{
get
{
return description;
}
set
{
description = value;
}
}
public override string ToString()
{
return "Employee ID: " + employeeNumber +
"Employee Name: " + name +
"Employee Hire Date: " + hiredate +
"Employee Monthly Salary: " + monthlySalary +
"Employee Description: " + description +
"Employee Department: " + department;
}
public void Print()
{
Console.WriteLine(this.ToString());
}
}
}

From the code you have posted, you don't call Console.WriteLine() anywhere. You have a Print method, but it won't be run until it is called. You need to create an instance of the class, then write the instance's ToString method to the console, or you can call the method Print on the instance.
In main you can do
Employee someone = new Employee(1, "John", "01/01/2016", 5000, "Engineer", "R&D");
Console.WriteLine(someone.ToString());
Console.ReadKey(); // Prevent the console from closing
In your Employee constructor, you should probably use the parameters it asks for, like this
this.employeeNumber = employeeNumber.
You should generally avoid having parameter names that match member variables/properties.
When you don't do anything special in your get and set properties, you can use auto properties
public int EmployeeNumber { get; set; }

Related

C# Using Custom class in a List

I have a class and I am using it inside a LIST
List<user> listWithCustomClass = List<user>();
myClass.cs
public class user
{
public user(string fullname, string city, string state, int age, int type)
{
name = fullname;
citi = city;
estate = state;
tipe = type;
}
private string name = string.Empty;
private string citi = string.Empty;
private string estate = string.Empty;
private int tipe = 0;
public string getFullname
{
get { return name; }
set { name = value;}
}
public string getCity
{
get { return citi; }
set { citi = value;}
}
public string getState
{
get { return state; }
set { state = value;}
}
public int getType
{
get { return type; }
set { type = value;}
}
}
How can I add a custom toString() without having to override generic toString(). I would like to add something like showDate().
For example, in a combobox I would like the output of the inserted information to be:
--> Hello, your name is {name} and your age is {age}
Like this:
foreach(var item in user)
{
user.ShowData();
}
Add this in your class:
public string ShowData()
{
return "Hello, your name is " + name + " and your age is " + age.ToString();
}
but you must also define age first. Which, following your style, would be:
private int age = 0;
and then in the constructor add:
this.age = age;
EDIT
foreach(var item in listWithCustomClass)
{
item.ShowData();
}

Inconsistent accessibility Error in Program

I'm currently trying to C# asp.net and this was one of my old labs that I never attended and I'm looking for help on it.
Basically I want to be able to store details of the person I input onto a webpage and then bring them up in the About page.
I keep getting the error:
Error 1: Inconsistent accessibility: field type 'System.Collections.Generic.IList<Lab5.Person>' is less accessible than field 'Lab5._Default.PresentPerson'
My code:
Class Person:
{
class Person
{
string age;
string name;
string dob;
string telNo;
string gender;
string address;
public string Age
{
get { return age; }
set { age = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public string DOB
{
get { return dob; }
set { dob = value; }
}
public string TelNo
{
get { return telNo; }
set { telNo = value; }
}
public string Gender
{
get { return gender; }
set { gender = value; }
}
public string Address
{
get { return address; }
set { address = value; }
}
public string enterPerson;
public Person(string name, string age, string dob, string telNo, string gender, string address)
{
Name = name;
Age = age;
DOB = dob;
TelNo = telNo;
Gender = gender;
Address = address;
}
public string PresentPerson()
{
return enterPerson = "Name: " + Name + "\n" + "Age: " + Age + "\n" + "Date of Birth: "
+ DOB + "\n" + "Telephone Number: " + TelNo + "\n" + "Gender: " + Gender + "\n" + "Address: "
+ Address;
}
}
}
Code behind the Default page:
{
public partial class _Default : Page
{
public static IList<Person> personList = new List<Person>();
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("You have successfully added a Person!");
personList.Add(new Person(TextBox1.Text, TextBox2.Text,
TextBox3.Text, TextBox4.Text, DropDownList1.Text, TextBox5.Text));
Session["Person"] = personList;
}
}
}
and code in the About page:
{
public partial class About : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["PersonList"] != null)
{
IList<Person> personList = (List<Person>)Session["PersonList"];
foreach (Person p in personList)
{
Response.Write(string.Format("Name :{0} and Age :{1}, DOB :{2}, TelNo, :{3}, Gender :{4}, Address :{5} ", p.Name, p.Age, p.DOB, p.TelNo, p.Gender, p.Address));
Response.Write("<br/>");
}
}
}
}
}
I know that there's probably a load of issues, but I am new to C#!
Your _Default page has a list of Person field. Person is internal, but _Default is public.
This is the inconsistency - clients of _Default would have access to these Person objects when they shouldn't. To fix, make Person public:
public class Person
{
....
You could, alternatively, make the field internal or private.
private static IList<Person> personList = new List<Person>();
You need to change class Person to public class Person
If something is public like the _Default class in your example every public thing on it also needs to be public. You can't have a public thing that is in fact private.

Calling other classes from Main in C#

Have two questions assigned for homework, both of the same form, so I'll post the first one:
"Create an Employee class. Items to include as data members are
employee number, name, date of hire, job description, department, and
monthly salary. The class is often used to display an alphabetical listing of all employees. Include appropriate constructors and properties. Override the
ToString ( ) method to return all data members. Create a second class
to test your Employee class."
I've created an Employee class with the proper variables, properties, and constructors, but am having trouble "testing" it through a second class. The code I have written runs without errors, but doesn't display anything (presumably the goal of the testing). Where am I going wrong in the calling section?
Employee info section:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EmployeeProgram
{
public class employee
{
private int employeeNumber;
private string name;
private string hiredate;
private int monthlySalary;
private string description;
private string department;
public employee(int employeeNumber, string name, string dateOfHire, int monthlySalary, string description, string department)
{
this.employeeNumber = 321;
this.name = "Alex";
this.hiredate = "01/02/15";
this.monthlySalary = 2500;
this.description = "Corporate grunt";
this.department = "Sales";
}
public int EmployeeNumber
{
get
{
return employeeNumber;
}
set
{
employeeNumber = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string Hiredate
{
get
{
return hiredate;
}
set
{
hiredate = value;
}
}
public int MonthlySalary
{
get
{
return monthlySalary;
}
set
{
monthlySalary = value;
}
}
public string Department
{
get
{
return department;
}
set
{
department = value;
}
}
public string Description
{
get
{
return description;
}
set
{
description = value;
}
}
public override string ToString()
{
return "Employee ID: " + employeeNumber +
"Employee Name: " + name +
"Employee Hire Date: " + hiredate +
"Employee Monthly Salary: " + monthlySalary +
"Employee Description: " + description +
"Employee Department: " + department;
}
public void Print()
{
Console.WriteLine(this.ToString());
}
}
"Calling section"
namespace employee
{
public class employeeApp
{
public static void Main()
{
EmployeeProgram.employee Employee = new EmployeeProgram.employee(321, "Alex", "1/02/15", 2500, "Corporate grunt", "Sales");
}
}
}
You need to invoke the Print() method of EmployeeProgram.employee in the main() method.
namespace employee
{
public class employeeApp
{
public static void Main()
{
EmployeeProgram.employee Employee = new EmployeeProgram.employee(321, "Alex", "1/02/15", 2500, "Corporate grunt", "Sales");
Employee.Print();
}
}
}
But as others have pointed out, there are a lot of issues with your code:
Class name should be in Pascal casing, i.e., start with a capital letter. Hence, it should be EmployeeProgram.Employee and not EmployeeProgram.employee
Private variables generally follow Camel casing, i.e., start with small letter. Hence, it should be meployee instead of Employee in your Main() method.
You can reduce the boilerplate code in your employee class by making use to auto-properties.
Parameter dateOfHire should be a DateTime object instead of string.
Parameters like monthlySalary are generally of type decimal and not int.

Can Derived class hide the accessors - Whats wrong with code below

I am trying to hide the accessors in derived class, is it valid? My system did not respond back on executing.
class BaseCS
{
private string name;
public string Name
{
get { return name; }
set { name = "Base " + value; }
}
}
class DerivedCS : BaseCS
{
public new string Name
{
set { Name = "Der " + value; }
get { return Name; }
}
}
public static void Main()
{
BaseCS one = new DerivedCS();
one.Name = "One";
Console.WriteLine("Name of object one is {0} ", one.Name);
((BaseCS)one).Name = "On1";
Console.WriteLine("Name of object one is {0} ", one.Name);
}
Shouldn't I expect output to be,
Name of object one is Base Der One
Name of object one is Base On1
This code:
public new string Name
{
set { Name = "Der " + value; }
get { return Name; }
}
Will lead to stack overflow, because Name in getters and setters will refer to Name in DerivedCS, not in BaseCS. Name property will call itself forever, until crashing. You need to use base.Name.
What you probably want is polymorphysm. You should make property virtual and override setter in the derived class.
If you do not use virtual properties, the following object will not behave the way you want:
BaseCS one = new DerivedCS();
one.Name = "name"; // base implementation is called
Changing property value in setter is usually a bad idea. Users expect the following contract to be followed:
var a = new A();
a.Foo = "bar";
Debig.Assert(a.Foo == "bar");
Here is what you should be doing
class BaseCS
{
private string name;
public virtual string Name
{
get { return name; }
set { name = "Base " + value; }
}
}
class DerivedCS : BaseCS
{
public override string Name
{
set { base.Name = "Der " + value; }
get { return base.Name; }
}
}
OR with a new keyword
class BaseCS
{
private string name;
public string Name
{
get { return name; }
set { name = "Base " + value; }
}
}
class DerivedCS : BaseCS
{
public new string Name
{
set { base.Name = "Der " + value; }
get { return base.Name; }
}
}
now you should create the object as the derived type to get your expected result
DerivedCS one = new DerivedCS();

issues calling another class

first class as follows:
public class employeeApp
{
public static void main()
{
EmployeeProgram.employee Employee = new EmployeeProgram.employee( );
}
public void employee(int employeeNumber, string name, string dateOfHire, int monthlySalary)
{
employeeNumber = 123;
name = Cody;
dateOfHire = 01/01/11;
monthlySalary = 2500;
}
}
second class as follows:
/*
* Mosbrucker_C_PRO_01 Author: Mosbrucker, Cody
* Creates a class for employee with data members;
* Employee number, name, date of hire, and monthly salary.
* ****************************************************/
public class employee
{
private int employeeNumber;
private string name;
private string dateOfHire;
private int monthlySalary;
public int EmployeeNumber
{
get
{
return employeeNumber;
}
set
{
employeeNumber = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string DateOfHire
{
get
{
return dateOfHire;
}
set
{
dateOfHire = value;
}
}
public int MonthlySalary
{
get
{
return monthlySalary;
}
set
{
monthlySalary = value;
}
}
public override string ToString()
{
return "Employee Id: " + employeeNumber +
"Employee Name: " + name +
"Employee Date of Hire: " + dateOfHire +
"Employee Monthly Salary: " + monthlySalary;
}
}
the problems I am getting are:
*In my employeeApp class "does not contain a "static" main method for suitable entry point"
*in my employeeApp class "the name Cody does not exist in current context
*in my employeeApp class relating to dateOfHire "cannot implicitly convert int to string
I'm doing this for a class and the assignment it:
Create a Employee class. Items to include as data members are employee number, name, date of hire, and monthly salary. Include appropriate constructors and properties. Override the ToString ( ) method to return all data members. Create a second class to test your Employee class.
Any help at all is greatly appreciated.
1.in C# we use Main(Capital M) so Main method should be :
static void Main()
2.You have to create the Constructor in your class employe
3.You have to assign String to the String variable but you are assigning date.
as below :
dateOfHire = 01/01/11;
in your constructor
4.Cody should be represented as String "Cody" in your Constructor
5.while assigning data to local variables in class use this to represent current object when assigning variable have same name
example : this.employeenumber=employeenumber;
file 1:
namespace employee
{
public class employeeApp
{
public static void Main()
{
EmployeeProgram.employee Employee = new EmployeeProgram.employee(123,"Cody","11/11/11",24567);//call your constructor
}
}
}
file 2:
/*
* Mosbrucker_C_PRO_01 Author: Mosbrucker, Cody
* Creates a class for employee with data members;
* Employee number, name, date of hire, and monthly salary.
* ****************************************************/
namespace EmployeeProgram
{
public class employee
{
private int employeeNumber;
private string name;
private string dateOfHire;
private int monthlySalary;
public employee(int employeeNumber, string name, string dateOfHire, int monthlySalary)
{
this.employeeNumber = 123;
this.name = "Cody";
this.dateOfHire = "01/01/11";
this.monthlySalary = 2500;
}
public int EmployeeNumber
{
get
{
return employeeNumber;
}
set
{
employeeNumber = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string DateOfHire
{
get
{
return dateOfHire;
}
set
{
dateOfHire = value;
}
}
public int MonthlySalary
{
get
{
return monthlySalary;
}
set
{
monthlySalary = value;
}
}
public override string ToString()
{
return "Employee Id: " + employeeNumber +
"Employee Name: " + name +
"Employee Date of Hire: " + dateOfHire +
"Employee Monthly Salary: " + monthlySalary;
}
}
}
Issue 1 - C# is case sensitive. Capitalize Main. The use of the public access modifier is not necessary and is generally not recommended for Main.
static void Main()
Issue 2 - For the second name = Cody; I guess you meant...name = "Cody";
Issue 3 - For the third issue you need to convert the int values to string by calling ToString() on int values. employeeNumber.ToString() and monthlySalary.ToString().
There are lots of issues here and they are all fairly basic. I recommend you use Google or explain why exactly you could not solve them. Otherwise it might appear you have not put forth the required effort to solve the problems yourself.
Issue 4 As for the I/O write problem you need to qualify using this keyword because of the naming conflict between your local variables and private fields:
public class employee
{
private int employeeNumber;
private string name;
private string dateOfHire;
private int monthlySalary;
public employee(int employeeNumber, string name, string dateOfHire, int monthlySalary)
{
this.employeeNumber = 123;//because you have naming collissions you need to use `this`
this.name = "Cody";
this.dateOfHire = "01 / 01 / 11";
this.monthlySalary = 2500;
}
public override string ToString()
{
return "Employee Id: " + employeeNumber +
"Employee Name: " + name +
"Employee Date of Hire: " + dateOfHire +
"Employee Monthly Salary: " + monthlySalary;
}
public void Print()
{
Console.WriteLine(this.ToString());
}
}
Then Main
static void Main(string[] args)
{
employee e = new employee(1,"","",0);//these values are ignored the way you set this up
e.Print();
Console.ReadLine();
}

Categories

Resources