Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Need help with C# classes
New Posts  All Forums:Forum Nav:

Need help with C# classes

post #1 of 27
Thread Starter 
I am having trouble understanding and using class in C#.

The program that I need to make needs two classes, one that has information that determines the pay of a worker and the other one to allow the user to enter their name and a different pay amount.

I am working on the first class now but cannot get my output to what I want and am kinda lost right now. I don't even know if I set this up the correct way. redface.gif
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CH4_Program_9
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee employee = new Employee();
            double  takeHomePay;

            takeHomePay = employee.TakeHome();
            employee.output(takeHomePay);

            Console.Read();
        }
    }

    public class Employee
    {
        private string employeeName = "TK";
        private const double totalSales = 0.07, fedTax = .18, retirement = 0.1, socSec = .06;
        private double weekSales = 28000, employPay, takeHome, taxAmount, retirementAmount, ssAmount;

        public Employee()
        {
        }

        public double EmployeePay
        {
            get 
            {
                return employPay;
            }
            set
            {
                employPay = weekSales * totalSales;
            }
        }

        public double TaxAmount
        {
            get
            {
                return taxAmount;
            }
            set
            {
                taxAmount = employPay * fedTax;
            }
        }

        public double RetirementAmount
        {
            get
            {
                return retirementAmount;
            }
            set
            {
                retirementAmount = employPay * retirement;
            }
        }

        public double SSAmount
        {
            get
            {
                return ssAmount;
            }
            set
            {
                ssAmount = employPay * socSec;
            }
        }

     public double TakeHome()
        { 
         takeHome = employPay - taxAmount - retirementAmount - ssAmount;
         return takeHome;
        }

        public void output(double takeHomePay)
        {
            Console.WriteLine("Employee Name: ", employeeName);
            Console.WriteLine("Commission: ", takeHomePay.ToString("C"));
        }
    }
}

I want to display the takehome amount and the employee name, but with this nothing shows for those lines. Can someone point me in the right direction?
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 950 Asus P6X58D-E Gigabyte GTX 770  12 GB GSkill 
Hard DriveOptical DriveOSMonitor
Seagate 1TB, Samsung 2TB TSSTcorp Windows 7 home Premium x64 Asus 24" 1920*1080 x 2 
KeyboardPowerCaseMouse
WASD XFX 650 watt Cooler Master HAF 932 Logitech G500 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 950 Asus P6X58D-E Gigabyte GTX 770  12 GB GSkill 
Hard DriveOptical DriveOSMonitor
Seagate 1TB, Samsung 2TB TSSTcorp Windows 7 home Premium x64 Asus 24" 1920*1080 x 2 
KeyboardPowerCaseMouse
WASD XFX 650 watt Cooler Master HAF 932 Logitech G500 
  hide details  
Reply
post #2 of 27
employPay - taxAmount - retirementAmount - ssAmount

These are all null when you call
Code:
public double TakeHome()
        { 
         takeHome = employPay - taxAmount - retirementAmount - ssAmount;
         return takeHome;
        }

You need to call the methods before they're going to have a value (best to call them in the constructor since they're stuff you want to be valid), and since TakeHome() is returning null, employee.output(takeHomePay) is not running.

Also, just a tip, but when you're naming methods, put a verb in there ie rather than "RetirementPay" setting retirementpay, have the method be "CalculateRetirementPay" so it's easier to follow.
The Obelisk
(19 items)
 
  
CPUMotherboardGraphicsGraphics
i7 2600k Asus Maximus IV Extreme-Z (Z68) Asus GTX 570 DirectCU II  EVGA 9800GT ECO 
RAMHard DriveHard DriveHard Drive
8GB Corsair Vengeance 1600mhz  WD Black 1TB OCZ Vertex 2 90GB OCZ Agility 4 128GB 
Hard DriveOSMonitorMonitor
Samsung 840 128GB Windows 7 Professional 64-bit ViewSonic VX2250 Asus VE248 
KeyboardPowerCaseMouse
Logitech G110 Corsair AX850 Corsair 800D Logitech G600 
Mouse PadAudio
C4 NGen Logitech G930 Headset 
  hide details  
Reply
The Obelisk
(19 items)
 
  
CPUMotherboardGraphicsGraphics
i7 2600k Asus Maximus IV Extreme-Z (Z68) Asus GTX 570 DirectCU II  EVGA 9800GT ECO 
RAMHard DriveHard DriveHard Drive
8GB Corsair Vengeance 1600mhz  WD Black 1TB OCZ Vertex 2 90GB OCZ Agility 4 128GB 
Hard DriveOSMonitorMonitor
Samsung 840 128GB Windows 7 Professional 64-bit ViewSonic VX2250 Asus VE248 
KeyboardPowerCaseMouse
Logitech G110 Corsair AX850 Corsair 800D Logitech G600 
Mouse PadAudio
C4 NGen Logitech G930 Headset 
  hide details  
Reply
post #3 of 27
Thread Starter 
How can I get the constructor to call the methods?
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 950 Asus P6X58D-E Gigabyte GTX 770  12 GB GSkill 
Hard DriveOptical DriveOSMonitor
Seagate 1TB, Samsung 2TB TSSTcorp Windows 7 home Premium x64 Asus 24" 1920*1080 x 2 
KeyboardPowerCaseMouse
WASD XFX 650 watt Cooler Master HAF 932 Logitech G500 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 950 Asus P6X58D-E Gigabyte GTX 770  12 GB GSkill 
Hard DriveOptical DriveOSMonitor
Seagate 1TB, Samsung 2TB TSSTcorp Windows 7 home Premium x64 Asus 24" 1920*1080 x 2 
KeyboardPowerCaseMouse
WASD XFX 650 watt Cooler Master HAF 932 Logitech G500 
  hide details  
Reply
post #4 of 27
Code:
public Employee()
        {
              EmployeePay();
              TaxAmount();
              RetirementAmount();
              SSAmount();

        }

That should work, can't remember if C# would require a "this." in front of them.
The Obelisk
(19 items)
 
  
CPUMotherboardGraphicsGraphics
i7 2600k Asus Maximus IV Extreme-Z (Z68) Asus GTX 570 DirectCU II  EVGA 9800GT ECO 
RAMHard DriveHard DriveHard Drive
8GB Corsair Vengeance 1600mhz  WD Black 1TB OCZ Vertex 2 90GB OCZ Agility 4 128GB 
Hard DriveOSMonitorMonitor
Samsung 840 128GB Windows 7 Professional 64-bit ViewSonic VX2250 Asus VE248 
KeyboardPowerCaseMouse
Logitech G110 Corsair AX850 Corsair 800D Logitech G600 
Mouse PadAudio
C4 NGen Logitech G930 Headset 
  hide details  
Reply
The Obelisk
(19 items)
 
  
CPUMotherboardGraphicsGraphics
i7 2600k Asus Maximus IV Extreme-Z (Z68) Asus GTX 570 DirectCU II  EVGA 9800GT ECO 
RAMHard DriveHard DriveHard Drive
8GB Corsair Vengeance 1600mhz  WD Black 1TB OCZ Vertex 2 90GB OCZ Agility 4 128GB 
Hard DriveOSMonitorMonitor
Samsung 840 128GB Windows 7 Professional 64-bit ViewSonic VX2250 Asus VE248 
KeyboardPowerCaseMouse
Logitech G110 Corsair AX850 Corsair 800D Logitech G600 
Mouse PadAudio
C4 NGen Logitech G930 Headset 
  hide details  
Reply
post #5 of 27
Thread Starter 
That doesn't work. I tried with a "this." also and that did not work either. It says they do not exist in the current context and with "this." it says they cannot be used like a method.
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 950 Asus P6X58D-E Gigabyte GTX 770  12 GB GSkill 
Hard DriveOptical DriveOSMonitor
Seagate 1TB, Samsung 2TB TSSTcorp Windows 7 home Premium x64 Asus 24" 1920*1080 x 2 
KeyboardPowerCaseMouse
WASD XFX 650 watt Cooler Master HAF 932 Logitech G500 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 950 Asus P6X58D-E Gigabyte GTX 770  12 GB GSkill 
Hard DriveOptical DriveOSMonitor
Seagate 1TB, Samsung 2TB TSSTcorp Windows 7 home Premium x64 Asus 24" 1920*1080 x 2 
KeyboardPowerCaseMouse
WASD XFX 650 watt Cooler Master HAF 932 Logitech G500 
  hide details  
Reply
post #6 of 27
In your program you do have the () after all the method names, right?
The Obelisk
(19 items)
 
  
CPUMotherboardGraphicsGraphics
i7 2600k Asus Maximus IV Extreme-Z (Z68) Asus GTX 570 DirectCU II  EVGA 9800GT ECO 
RAMHard DriveHard DriveHard Drive
8GB Corsair Vengeance 1600mhz  WD Black 1TB OCZ Vertex 2 90GB OCZ Agility 4 128GB 
Hard DriveOSMonitorMonitor
Samsung 840 128GB Windows 7 Professional 64-bit ViewSonic VX2250 Asus VE248 
KeyboardPowerCaseMouse
Logitech G110 Corsair AX850 Corsair 800D Logitech G600 
Mouse PadAudio
C4 NGen Logitech G930 Headset 
  hide details  
Reply
The Obelisk
(19 items)
 
  
CPUMotherboardGraphicsGraphics
i7 2600k Asus Maximus IV Extreme-Z (Z68) Asus GTX 570 DirectCU II  EVGA 9800GT ECO 
RAMHard DriveHard DriveHard Drive
8GB Corsair Vengeance 1600mhz  WD Black 1TB OCZ Vertex 2 90GB OCZ Agility 4 128GB 
Hard DriveOSMonitorMonitor
Samsung 840 128GB Windows 7 Professional 64-bit ViewSonic VX2250 Asus VE248 
KeyboardPowerCaseMouse
Logitech G110 Corsair AX850 Corsair 800D Logitech G600 
Mouse PadAudio
C4 NGen Logitech G930 Headset 
  hide details  
Reply
post #7 of 27
Thread Starter 
Yes
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 950 Asus P6X58D-E Gigabyte GTX 770  12 GB GSkill 
Hard DriveOptical DriveOSMonitor
Seagate 1TB, Samsung 2TB TSSTcorp Windows 7 home Premium x64 Asus 24" 1920*1080 x 2 
KeyboardPowerCaseMouse
WASD XFX 650 watt Cooler Master HAF 932 Logitech G500 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 950 Asus P6X58D-E Gigabyte GTX 770  12 GB GSkill 
Hard DriveOptical DriveOSMonitor
Seagate 1TB, Samsung 2TB TSSTcorp Windows 7 home Premium x64 Asus 24" 1920*1080 x 2 
KeyboardPowerCaseMouse
WASD XFX 650 watt Cooler Master HAF 932 Logitech G500 
  hide details  
Reply
post #8 of 27
Ah, my apologies for visual debugging, I overlooked the simplest problem.
Code:
public void output(double takeHomePay)
        {
            Console.WriteLine("Employee Name: "+ employeeName);
            Console.WriteLine("Commission: "+ takeHomePay.ToString("C"));
        }

Combining strings is a plus sign, not a comma.
The Obelisk
(19 items)
 
  
CPUMotherboardGraphicsGraphics
i7 2600k Asus Maximus IV Extreme-Z (Z68) Asus GTX 570 DirectCU II  EVGA 9800GT ECO 
RAMHard DriveHard DriveHard Drive
8GB Corsair Vengeance 1600mhz  WD Black 1TB OCZ Vertex 2 90GB OCZ Agility 4 128GB 
Hard DriveOSMonitorMonitor
Samsung 840 128GB Windows 7 Professional 64-bit ViewSonic VX2250 Asus VE248 
KeyboardPowerCaseMouse
Logitech G110 Corsair AX850 Corsair 800D Logitech G600 
Mouse PadAudio
C4 NGen Logitech G930 Headset 
  hide details  
Reply
The Obelisk
(19 items)
 
  
CPUMotherboardGraphicsGraphics
i7 2600k Asus Maximus IV Extreme-Z (Z68) Asus GTX 570 DirectCU II  EVGA 9800GT ECO 
RAMHard DriveHard DriveHard Drive
8GB Corsair Vengeance 1600mhz  WD Black 1TB OCZ Vertex 2 90GB OCZ Agility 4 128GB 
Hard DriveOSMonitorMonitor
Samsung 840 128GB Windows 7 Professional 64-bit ViewSonic VX2250 Asus VE248 
KeyboardPowerCaseMouse
Logitech G110 Corsair AX850 Corsair 800D Logitech G600 
Mouse PadAudio
C4 NGen Logitech G930 Headset 
  hide details  
Reply
post #9 of 27

Do you know what the difference between a property and a method is?

 

Quote:
Originally Posted by -Apocalypse- View Post

Code:
public Employee()
        {
              EmployeePay();
              TaxAmount();
              RetirementAmount();
              SSAmount();
        }
That should work, can't remember if C# would require a "this." in front of them.

 

This is getting there.  You're calling a method when you've written properties.  Secondly, this code above doesn't actually do anything with the variables you want to call.

600t
(16 items)
 
  
CPUMotherboardGraphicsRAM
2600k Z77 Extreme4 7970 4x4GB Corsair Arctic White LP Vengeance @ 1600 
Hard DriveHard DriveHard DriveOptical Drive
crucial m4 Caviar Black Caviar Green LG Blu-Ray 
CoolingOSMonitorMonitor
H100 Windows 7 Dell U2412m Dell 2407WFP 
KeyboardPowerCaseMouse
Saitek Eclipse Seasonic x-760 600t Logitech G700 
  hide details  
Reply
600t
(16 items)
 
  
CPUMotherboardGraphicsRAM
2600k Z77 Extreme4 7970 4x4GB Corsair Arctic White LP Vengeance @ 1600 
Hard DriveHard DriveHard DriveOptical Drive
crucial m4 Caviar Black Caviar Green LG Blu-Ray 
CoolingOSMonitorMonitor
H100 Windows 7 Dell U2412m Dell 2407WFP 
KeyboardPowerCaseMouse
Saitek Eclipse Seasonic x-760 600t Logitech G700 
  hide details  
Reply
post #10 of 27
Quote:
Originally Posted by -Apocalypse- View Post

Ah, my apologies for visual debugging, I overlooked the simplest problem.
Code:
public void output(double takeHomePay)
        {
            Console.WriteLine("Employee Name: {0}", employeeName);
            Console.WriteLine("Commission: {0:C}", takeHomePay);
        }
Combining strings is a plus sign, not a comma.

 

+ for String concatenation.  Commas for String Format.

600t
(16 items)
 
  
CPUMotherboardGraphicsRAM
2600k Z77 Extreme4 7970 4x4GB Corsair Arctic White LP Vengeance @ 1600 
Hard DriveHard DriveHard DriveOptical Drive
crucial m4 Caviar Black Caviar Green LG Blu-Ray 
CoolingOSMonitorMonitor
H100 Windows 7 Dell U2412m Dell 2407WFP 
KeyboardPowerCaseMouse
Saitek Eclipse Seasonic x-760 600t Logitech G700 
  hide details  
Reply
600t
(16 items)
 
  
CPUMotherboardGraphicsRAM
2600k Z77 Extreme4 7970 4x4GB Corsair Arctic White LP Vengeance @ 1600 
Hard DriveHard DriveHard DriveOptical Drive
crucial m4 Caviar Black Caviar Green LG Blu-Ray 
CoolingOSMonitorMonitor
H100 Windows 7 Dell U2412m Dell 2407WFP 
KeyboardPowerCaseMouse
Saitek Eclipse Seasonic x-760 600t Logitech G700 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Need help with C# classes