Dinesh Persaud
Dinesh Persaud

Reputation: 155

Two classes in one .cs file

im new to this, maybe im doing something wrong. I have on .cs file name PayrollDetails.cs . In it i created two classes (1) get GrossPay and (2) PayrollDetails. I have some getters and setters methods there in the GrossPay class and i would like to use them. Why is it i am not getting it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace SSPModel
{
public class getGrossPay {
       public List<BasicPay> BPList;
       private List<Allowance> AlGradeList;
       private List<Allowance> AlPostList;
       private List<AdditionalEarnings> AdEarnList;
       private List<Benefits> BenList;

       public void setBPList(List<BasicPay> a)
       {
           BPList = a;

       }
       public List<BasicPay> GetBPList() {

           return BPList;
       }

       public List<Allowance> GetAlGradeList()
       {
           return AlGradeList;
       }

       public void setAlGradeList(List<Allowance> a)
       {
           AlGradeList = a;
       }

       public List<Allowance> GetAlPostList()
       {
           return AlPostList;
       }

       public void setAlPostList(List<Allowance> a)
       {
           AlPostList = a;
       }
       public List<AdditionalEarnings> GetAdEarnList()
       {
           return AdEarnList;
       }

       public void setAdEarnList(List<AdditionalEarnings> a)
       {
           AdEarnList = a;
       }


       public List<Benefits> GetBenList()
       {
           return BenList;
       }

       public void setBenList(List<Benefits> a)
       {
           BenList = a;
       }
       public void ssss(getGrossPay inst)
       {
        List<BasicPay> a =   inst.GetBPList();
        for (int i = 0; i < a.Count(); i++)
        {
            Debug.WriteLine(a.ElementAtOrDefault(i));

        }

       }
       }
      }

    public class PayrollDetails
    {

        public static void doSomething()
        {
             // i am not getting to call any method from the class getGrossPay.
    }
}

Also, on somewhat an unrelated question, i need some advice on best practice. I have other .cs files (e.g BasicPay.cs, Allowance.cs) where i compute values. I then would call the setter method in the GrossPay class (this works perfectly fine) but i would like to use the get method in another location (or .cs file). I basically wanna set it one place and then use getter in a different location. If i created a new instance of it to use the getter method in another .cs or location, it would result in null. Any ideas of how to go about this? I basically have a long list of functions i want to do, when one i completed, i use the setter and so forth and when ALL is complete call the getter method for ALL that has been set and then do my calculation. Any help would be much appreciated. A more experience programmer may have a better way of going about doing this.

Upvotes: 0

Views: 3436

Answers (2)

Erik Noren
Erik Noren

Reputation: 4339

doSomething is a static method in PayrollDetails. The methods and members in getGrossPay are not static, they are instance methods and members. You will have to instantiate a new getGrossPay before you can call methods on them.

public static void doSomething()
{
  var grossPay = new getGrossPay();
  var earnList = grossPay.GetAdEarnList();
  //etc.
}

Upvotes: 1

Zyo
Zyo

Reputation: 2068

There is nothing illegal about having 2 classes in 1 file. In fact you could make entire program in 1 file.

It's all about readability. It's a convenience to have 1 file per class, the file name is usually the class name.

And as the project goes bigger you have namespace to split all the code into module.

Upvotes: 1

Related Questions