I have NO idea why C# is so difficult for me when C++ is a breeze. I want to preface this post with this:

Yes, this is for a class still. But I'm only coming to people more knowledgable than I because... I just don't get it. I've read my chapter on this 5 or 6 times, looked at the examples, did the practice excercises, etc. It just goes over my head.

This is the basic code I wrote out to do a payroll program using diffent methods.

using SC = System.Console;
using System;
public class Lab15
    //Main method begins
    public static void Main()
        string hoursWorkedStr, payRateStr, empName;
        double hoursWorked, payRate, grossPay, stateTax, federalTax, ficaTax, netPay;

        SC.WriteLine("Lab 15");

        //Priming read
        SC.WriteLine("\nPlease input employee name (or Quit to end program): ");
        empName = Console.ReadLine();

        while(empName != "Quit" && empName != "quit")
            SC.WriteLine("\nPlease input hours worked:\t");
            hoursWorkedStr = Console.ReadLine();
            hoursWorked = Convert.ToDouble(hoursWorkedStr);

            SC.WriteLine("\nPlease input rate of pay:");
            payRateStr = Console.ReadLine();
            payRate = Convert.ToDouble(payRateStr);

            grossPay = CalcGrossPay(hoursWorked, payRate);

            stateTax = CalcStateTax(grossPay);
            federalTax = CalcFederalTax(grossPay);
            ficaTax = CalcFICATax(grossPay);

            netPay = CalcNetPay(grossPay);

            SC.WriteLine("\nGross pay is: {0,8}", grossPay.ToString("C2"));
            SC.WriteLine("State tax is: {0,8}", stateTax.ToString("C2"));
            SC.WriteLine("Federal tax is: {0,8}", federalTax.ToString("C2"));
            SC.WriteLine("FICA tax is: {0,8}", ficaTax.ToString("C2"));
            SC.WriteLine("Net pay is: {0,8}", netPay.ToString("C2"));

            //Continue priming read logic
            SC.WriteLine("\nPlease input employee name (or Quit to end program): ");
            empName = Console.ReadLine();
        //Main method ends

        //End statement
        SC.WriteLine("\nEnd of Lab 15\n");

    //Gross pay method
    public static double CalcGrossPay(double hoursWorked, double payRate)
        double grossPay = hoursWorked * payRate;
        return grossPay;

    //State tax method
    public static double CalcStateTax(double grossPay)
        double stateTax = grossPay * .04;
        return stateTax;

    //Federal tax method
    public static double CalcFederalTax(double grossPay)
        double federalTax = grossPay * .23;
        return federalTax;

    //FICA tax method
    public static double CalcFICATax(double grossPay)
        double ficaTax = grossPay * .14;
        return ficaTax;

    //Net pay method
    public static double CalcNetPay(double grossPay)
        double taxes = CalcStateTax(grossPay)+CalcFederalTax(grossPay)+CalcFICATax(grossPay);
        double netPay = grossPay - taxes;
        return netPay;

My specs say "Calculation of gross pay should be done in a method that uses two value parameters and returns the gross pay. Calculation of State Tax should be done in a void method that uses an output parameter for State Tax and a value parameter for the gross pay. Other methods should be void and use reference parameters. Again, do not use any global variables."

I'm not asking for anyone to DO my work. I'm just asking for someone to explain to me HOW to do my work. I'm always saying me knowing how is more important than my grade, but I'm drowning.

The spec tells you to use void methods for everything except for the calculation of gross pay, and they are referring to the return type. They want you to use the out and ref modifiers to "return" the value to the calling method. Check the links for more info, but I'll give you a quick explanation. In C++ terms, it is basically passing in a pointer to the object, instead of the object itself (or a pointer of a pointer, in the case of classes). They are essentially the same, in that the variable passed into the method, will take on whatever value the called method gives it, for instance:

public static void Main()
    int n = 1;
    Foo(ref n);

public static void Foo(ref int n)

This program will output 2. Note that any method call must explicitly use the ref modifier. out works similar to this; however, with out, the method assumes the value was never instantiated. In other words, the method must assign a value before it can use it for anything. Hope this helps. Feel free to ask if you need any clarification.

This article has been dead for over six months. Start a new discussion instead.