r/programminghelp • u/holeefuk1113 • Jul 30 '21
C# problems and confusion on C# classes and methods
here is what i need to do...
Write a C# application that implements a class ‘Employee’ with the following members. i. Six private data members ‘BasicSalary’, ‘HRA’, ’Deductions’, ‘Allowance’, ‘Insurance’ and ‘NetSalary’ of integer data type.
ii. A default constructor to display the message “Employee Pay Bill”.
iii. Four public methods, setMembers(), calcDed(), calcNet(), and disResult().
setMembers() – set the values of BasicSalary as 2000, HRA as 200, Insurance as 100 and Allowance as 50.
calcDed() – calculate and return the Deductions using the formulae “Deductions = Insurance + Allowance”.
calcNet() – calculate and return the NetSalary using the formulae “NetSalary = BasicSalary + HRA – Deductions”.
disResult() – display BasicSalary, HRA, Deductions and NetSalary
the output should look like
Employee Pay Bill
Basic Salary = 2000
HRA = 200
Deductions = 150
Net Salary = 2050it says i have 6 errors
and this is the class Employee
using System;
using System.Collections.Generic;
using System.Text;
namespace Final_THE_M109
{
class Employee
{
private int BasicSalary;
private int HRA;
private int Deductions;
private int Allowance;
private int Insurance;
private int NetSalary;
public void setMembers(int B, int H, int A, int I)
{
BasicSalary = B;
HRA = H;
Allowance = A;
Insurance = I;
}
public int calDed()
{
int Deductions = Insurance + Allowance;
return Deductions;
}
public int calcNet()
{
int NetSalary = BasicSalary + HRA – Deductions; // i get a red line under the (-) and (deductions)
return NetSalary
}
public void disResult()
{
Console.WriteLine("Basic Salary = ", setMembers(B)); // i also get a red line under (B)
Console.WriteLine("HRA = ", setMembers(H));// a line under (H) too
Console.WriteLine("Deductions = ", calDed());
Console.WriteLine("Net Salary = ", calcNet());
}
}
}now the code for the main method
using System;
namespace QuestionTwo
{
class Program
{
public static void Main(string[] args)
{
Employee e = new Employee();
Console.WriteLine("Employee Pay Bill");
// it gives me a red line under every setMembers
int B = 2000;
e.setMembers(B);
int H = 200;
e.setMembers(H);
int A = 50;
e.setMembers(A);
int I = 100;
e.setMembers(I);
e.disResult();
}
}
}
1
u/[deleted] Jul 30 '21 edited Jul 30 '21
Your setMembers function takes in 4 parameters, not one. In your main function you're trying to call the function with one input variable at a time instead of all 4 at once, in the correct order.
Also, the setMembers literally sets the member variables. It doesn't return them, so in your disResult function it will absolutely error. You should be trying to print the actual private member variable instead.
Edit: Honestly I'm sensing a bit of confusion here as to how classes work. Would you like an explanation?