I have 2 programs from 2 different folders, currently, I have a class in SectionA.cs (namespace SectionA, class name Employee) and I want to reuse a method from the class in SectionB.cs (namespace SectionB), but I do not know how reference a class from SectionB.cs. I heard you have to use using SectionA;
but it gives me this error
The type or namespace name 'SectionA' could not be found (are you missing a using directive or an assembly reference?) [SectionB]
The picture below shows the file directory, may I know how to reference the class in SectionA.cs to use it in SectionB.cs? Thank you in advance.
SectionA.cs
using System;
using System.IO;
using System.Collections.Generic;
namespace SectionA
{
public delegate void generateDelegate(List<Employee> EmployeeList);
public class Employee
{
public string Nric, FullName, Salutation, Designation, Department, MobileNo, HireType;
public DateTime Start_Date;
public double Salary, MonthlyPayout;
public Employee(string Nric, string FullName, string Salutation, DateTime Start_Date, string Designation, string Department, string MobileNo, string HireType, double Salary)
{
this.Nric = Nric;
this.FullName = FullName;
this.Salutation = Salutation;
this.Start_Date = Start_Date;
this.Designation = Designation;
this.Department = Department;
this.MobileNo = MobileNo;
this.Salary = Salary;
this.MonthlyPayout = 0.0;
}
static public string CorpAdmin(List<Employee> EmployeeList)
{
string result = "";
foreach (Employee employee in EmployeeList)
{
result += employee.FullName + "," + employee.Designation + "," + employee.Department + "\n";
}
return result;
}
static public string Procurement(List<Employee> EmployeeList)
{
string result = "";
foreach (Employee employee in EmployeeList)
{
result += employee.Salutation + "," + employee.FullName + "," + employee.MobileNo + "," + employee.Designation + "," + employee.Department + "\n";
}
return result;
}
static public string IT(List<Employee> EmployeeList)
{
string result = "";
foreach (Employee employee in EmployeeList)
{
result += employee.Nric + "," + employee.FullName + "," + employee.Start_Date + "," + employee.Department + "," + employee.MobileNo + "\n";
}
return result;
}
static public List<Employee> readHRMasterList()
{
List<Employee> EmployeeList = new List<Employee>();
string direct = System.IO.Directory.GetCurrentDirectory();
string path = "";
string[] pathList = direct.Split("\\");
for (int i = 0; i < pathList.Length - 1; i++) { path = path + pathList[i] + "\\"; }
path += "HRMasterlist.txt";
using (StreamReader sr = File.OpenText(path))
{
string s;
while ((s = sr.ReadLine()) != null)
{
string[] Emp = s.Split("|");
EmployeeList.Add(new Employee(Emp[0], Emp[1], Emp[2], Convert.ToDateTime(Emp[3]), Emp[4], Emp[5], Emp[6], Emp[7], Convert.ToDouble(Emp[8])));
}
}
return EmployeeList;
}
static public void generateInfoForCorpAadmin(List<Employee> Employee_list)
{
string result = CorpAdmin(Employee_list);
string path = @"CorporateAdmin.txt";
string text = "";
if (!File.Exists(path)) { using (StreamWriter sw = File.CreateText(path)) { } }
using (StreamReader sr = File.OpenText(path))
{
string s;
while ((s = sr.ReadLine()) != null)
{
if (s != "") { text = text + s + "\n"; }
}
}
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine(text + result);
}
}
static public void generateInfoForProcurement(List<Employee> Employee_list)
{
string result = Procurement(Employee_list);
string path = @"Procurement.txt";
string text = "";
if (!File.Exists(path)) { using (StreamWriter sw = File.CreateText(path)) { } }
using (StreamReader sr = File.OpenText(path))
{
string s;
while ((s = sr.ReadLine()) != null)
{
if (s != "") { text = text + s + "\n"; }
}
}
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine(text + result);
}
}
static public void generateInfoForITDepartment(List<Employee> Employee_list)
{
string result = IT(Employee_list);
string path = @"ITDepartment.txt";
string text = "";
if (!File.Exists(path)) { using (StreamWriter sw = File.CreateText(path)) { } }
using (StreamReader sr = File.OpenText(path))
{
string s;
while ((s = sr.ReadLine()) != null)
{
if (s != "") { text = text + s + "\n"; }
}
}
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine(text + result);
}
}
static void Main(string[] args)
{
List<Employee> EmployeeList = new List<Employee>();
EmployeeList = readHRMasterList();
generateDelegate del1 = generateInfoForCorpAadmin;
generateDelegate del2 = generateInfoForProcurement;
generateDelegate del3 = generateInfoForITDepartment;
generateDelegate del = del1 + del2 + del3;
del(EmployeeList);
}
}
}
SectionB.cs
using System;
using SectionA;
namespace SectionB
{
class SectionB
{
static void Main(string[] args)
{
}
}
}