1

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.

enter image description here

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)
        {
        }
    }
}
3dsss
  • 37
  • 2
  • 12
  • Please show the code for both `.cs` files. – Noah Stahl Jul 25 '21 at 14:47
  • Ar you using Visual Studio Code? Then see also https://stackoverflow.com/questions/42000798/how-do-i-add-assembly-references-in-visual-studio-code on how to add a reference. – Klaus Gütter Jul 25 '21 at 14:48
  • @NoahStahl I have added the code for both cs files, looking to use the method readHRMasterList in SectionB.cs – 3dsss Jul 25 '21 at 14:59
  • `#include "../SectionA/SectionA.cs"` will add the reference to the code of SectionA. That way, when you add the line below for `#using SectionA` that namespace will be recognized. – Dash Jul 25 '21 at 15:52

3 Answers3

3

You need to add a reference from SectionB project to SectionA project and then the using should work.

Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116
  • Hi, thanks for the answer. May I ask, how am i supposed to add a reference from SectionB project to SectionA project? – 3dsss Jul 25 '21 at 14:51
  • @3dsss Yes, I see some of the answers already wrote how to do it. A good detailed explanation you can find here: https://stackoverflow.com/questions/42000798/how-do-i-add-assembly-references-in-visual-studio-code – Misha Zaslavsky Jul 25 '21 at 14:54
  • but I have a question, if I add the reference, will the person I send the file to have to add the reference as well, or he does not have to as I've already reference it? – 3dsss Jul 25 '21 at 15:02
  • @3dsss He will not have to. When you add a reference it updates the code of the .`csproj` file. So that the one who you will send will not need to do it as well. – Misha Zaslavsky Jul 25 '21 at 15:06
  • But you will have to deploy both assemblies, of course. – Klaus Gütter Jul 25 '21 at 15:09
2

You can try to open SectrionB.csproj file of the project and add project reference like this:

<ItemGroup>
    <ProjectReference Include = "../SectionA.csproj" />
</ItemGroup>

after this you would be able to use it in your code

using ....
.....
Serge
  • 40,935
  • 4
  • 18
  • 45
1

If you open it in Visual Studio, you can right click SectionB -> References -> Add Reference and add select the reference for SectionA.

Add reference

cannelle28
  • 168
  • 5
  • 17
  • Hi, thanks for the answer, I am using Visual Studio Code and not Visual Studio, hence I am unable to do that – 3dsss Jul 25 '21 at 14:51