r/programminghelp • • 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().

  1. setMembers() – set the values of BasicSalary as 2000, HRA as 200, Insurance as 100 and Allowance as 50.

  2. calcDed() – calculate and return the Deductions using the formulae “Deductions = Insurance + Allowance”.

  3. calcNet() – calculate and return the NetSalary using the formulae “NetSalary = BasicSalary + HRA – Deductions”.

  4. disResult() – display BasicSalary, HRA, Deductions and NetSalary

the output should look like

Employee Pay Bill
Basic Salary = 2000
HRA = 200
Deductions = 150
Net Salary = 2050

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

12 comments sorted by

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?

1

u/holeefuk1113 Jul 30 '21

yes please, i actually thought the same about the parameters but i didn't know if i should just write them one at a time or if there was a more efficient way

1

u/[deleted] Jul 30 '21

Classes are quite literally Object Definitions. They are blueprints.

If you define a class then you can create new objects based on that blueprint in just the same way you're doing in your main function by creating a new Employee.

The variables and functions inside the class, in your case BasicSalary, HRA, calDed, calcNet etc exist inside every copy of that class.

If they are private, then they are hidden from the outside world and can only be touched from inside the class.

If they are public, then they are exposed to the outside world and can be read/modified directly but can also be touched from inside the class.

Now, as to your actual errors: The error in your calcNet function is because, somehow, the character you used for the minus symbol actually isn't a minus symbol it's a dash. Just delete that character and use - and you'll be fine.

The errors you're seeing in disResult are because what you're telling the compiler to do:

Console.WriteLine("Basic Salary = ", setMembers(B));

You're asking it to attach the RESULT of that function (setMembers) to the string, then send that string to the console.

You defined the setMembers() function as:

public void setMembers(int B, int H, int A, int  I)

this means it's a public function (can be seen and used from outside)

that it returns void (nothing)

and that it takes in 4 properties (B, H, A, I).

In your call: setMembers(B), not only are you NOT giving it the rest of the values it wants (H, A, and I), you're asking the return value to be attached to a string. In this case, the return is void so there's nothing to attach, and that's an error.

The fix for this is to understand that the disResult function is part of the class so it can see all the private variables, and since it can see those, it can use those.

Console.WriteLine("Basic Salary = ", BasicSalary);

another way you can write that, to better understand it is

Console.WriteLine("Basic Salary = ", this.BasicSalary);

('this' means me, myself, aka this class)

1

u/holeefuk1113 Jul 30 '21

i tried different methods but i can't seem to find the right way to do it

here is what I tried both ended up wrong 1 because it can't work without overloading the method and the other is that i just thought it may work but it didn't.

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 Employee() { Console.WriteLine("Employee Pay Bill"); } public void setMembers(int B,int H, int I, int A) { BasicSalary = B; HRA = H; Insurance = I; Allowance = A; } public int getMembers() { return BasicSalary; return HRA; return Insurance; return Allowance; } // public void setMembers(int H) // { // HRA = H; //} //public int getMembers() //{ // return HRA; //} public int calDed() { int Deductions = Insurance + Allowance; return Deductions; } public int calcNet() { NetSalary = BasicSalary + HRA - Deductions; return NetSalary; } public void disResult() { Console.WriteLine("Basic Salary = ", this.BasicSalary); Console.WriteLine("HRA = ", HRA); Console.WriteLine("Deductions = ", calDed()); Console.WriteLine("Net Salary = ", calcNet()); } } }

1

u/[deleted] Jul 30 '21

You can't return multiple values from a single function in C#.

You either return an array of values:

return new int[4]{BasicSalary, HRA, Insurance, Allowance}

or more normally, you create one function to return the value, per value (so 4 functions, getBasicSalary, getHRA, getInsurance, getAllowance)

But, like I said in my above post, you don't need to do any of this, since your print function is part of your class and can just view the variables directly.

1

u/holeefuk1113 Jul 30 '21

yeah i got that which is why one of the ways I used was

    public void setMembers(int B,int H, int I, int A)
    {
        BasicSalary = B;
        HRA = H;
        Insurance = I;
        Allowance = A;
    }
    public int getMembers()
    {
        return BasicSalary;
        return HRA;
        return Insurance;
        return Allowance;
    }
   // public void setMembers(int H)
   // {
     //   HRA = H;
    //}
    //public int getMembers()
    //{
     //   return HRA;
    //}
     // so basically i was going to have 1 setMember() 1 getMemeber as you can see but that wouldn't work unless we use method overloading. which wouldn't be as simple as it could.

which is why i am intrested in the way you get the value, can you extrapolate on that?

as you can see above i need to use setMember() method in my code

2

u/[deleted] Jul 30 '21

Your SetMembers function is fine.

You do not need functions to get the members. It's not asked for in your instructions. The function you have that prints everything out, can do so in the manner I outlined in my other post.

1

u/holeefuk1113 Jul 30 '21

do you mean the

Console.WriteLine("Basic Salary = ", this.BasicSalary);
        Console.WriteLine("HRA = ", HRA);
        Console.WriteLine("Deductions = ", calDed());
        Console.WriteLine("Net Salary = ", calcNet());

if so then what do I do in the setMember() method?

do i keep it as it used ti be or do I independatnly write setMember() for each value?

btw, thank you for being patient with me and helping me out

2

u/[deleted] Jul 30 '21

setMembers is the function you will call from your main function, to set the variables inside of the new object you created.

Once those variables are set, the functions inside the class, can read them and print them out without any issue.

So, if you've done everyting right, your main function should look like this:

Employee e = new Employee();
e.setMembers(2000, 200, 50, 100);
e.disResult();

1

u/holeefuk1113 Jul 30 '21 edited Jul 30 '21

it worked, for the most part, it actually prints

Employee Pay Bill
Basic Salary =
HRA = 
Deductions = 
Net Salary = 
but as you can see I'm not getting actual numbers, here is my code write now.

using System;

using System.Collections.Generic;

using System.Text;

namespace QuestionTwo

{

class Employee

{

private int BasicSalary;

private int HRA;

private int Deductions;

private int Allowance;

private int Insurance;

private int NetSalary;

public Employee()

{

Console.WriteLine("Employee Pay Bill");

}

public void setMembers(int B,int H, int I, int A)

{

BasicSalary = B;

HRA = H;

Insurance = I;

Allowance = A;

}

public int calDed()

{

int Deductions = Insurance + Allowance;

return Deductions;

}

public int calcNet()

{

NetSalary = BasicSalary + HRA - Deductions;

return NetSalary;

}

public void disResult()

{

Console.WriteLine("Basic Salary = ", this.BasicSalary); Console.WriteLine("HRA = ", this.HRA); Console.WriteLine("Deductions = ", calDed()); Console.WriteLine("Net Salary = ", calcNet()); } } }

and

using System;

namespace QuestionTwo

{

class Program

{

public static void Main(string[] args)

{

Employee emp1 = new Employee(); emp1.setMembers(2000, 200, 50, 200); emp1.disResult();

}

}

}

→ More replies (0)