Calling other classes from Main in C# - 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.

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();
}

Calling overridden method

Below shows my 3 classes: Person, Employee and SalaryEmployee.
Each class (from the right) inherits the class from the left side. E.g: SalaryEmployee inherits from Employee.
class Person
{
private string address;
private string name;
public Person(string givenName, string givenAddress)
{
name = givenName;
address = givenAddress;
}
public virtual void outputName()
{
MessageBox.Show("My name is " + name);
}
public void outputAddress()
{
MessageBox.Show("My address is " + address);
}
public void setName(string newName)
{
name = newName;
}
public void setAddress(string newAddress)
{
address = newAddress;
}
}
class Employee : Person
{
private string NINumber;
public Employee(string givenName, string givenAddress) : base(givenName, givenAddress)
{
}
public void setNINumber(string givenNumber)
{
NINumber = givenNumber;
}
public void getNINumber()
{
MessageBox.Show("My National Insurance Number is " + NINumber);
}
}
class SalaryEmployee : Employee
{
private string name;
private string address;
public SalaryEmployee(string givenName, string givenAddress) : base(givenName, givenAddress)
{
name = givenName; address = givenAddress;
}
public override void outputName()
{
MessageBox.Show("My name is " + "S_" + name);
}
}
OOP is fairly new to me and I am trying to get the hang of inheritance. The problem I am facing is I have created an object called 'PersonFive':
SalaryEmployee personFive = new SalaryEmployee("Bob", "North Pole");
// Ignore the address 'North pole' I have used as a parameter, it's just used for this example.
I am able to call the .outputName() method, the output will be:
"My name is Bob"
However, the base method is called instead of the overridden method. The output I wanted it to give me was "My name is S_Bob"
It's working for me, you might didn't post the actual code that makes the error.

How do I test a simple class?

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; }

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.

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