Calling methods from another project

Roust_m

Member
Joined
May 29, 2022
Messages
9
Programming Experience
Beginner
Hi,

I have two projects in my solution, one for console app and another for a web app. I need to re-use some code from the console app in my web app. I've pasted classes from my console app into my web app as links.

These are the methods I have in my web app:
Console app methods:
namespace Export
{
    public partial class XeroExport: ApiWrapper
    {
        public void DBExport(DateTime reportDate, int entityId, AccountingApi apiInstance, string xeroTenantId)
        {
        ...
        }

        public void Cleanup(Nullable<DateTime> periodStart, Nullable<DateTime> periodEnd, int entityId, String tableName, String dataDB)
        {
        ...
        }
    }
}

namespace Export
{
    public partial class XeroExport
    {
         public async Task Call_BalanceSheet(DateTime reportDate, int entityID, DateTime periodStart, DateTime periodEnd, AccountingApi apiInstance, string xeroTenantId, string accessToken, string connectDB, string dataDB)
        {
        ...
        }

    }
}
namespace Export
{
    public partial class XeroExport
    {
        public async Task<AccountingApi> Xero_Connect(string clientId, string clientSecret, int entityID)
        {
        ...
        }
    ...
    }
    ...
}

Below is what I have in my web app project. I need to call the following methods:
Cleanup
Call_BalanceSheet
XeroExport
I am getting errors like: "Error CS0103 The name 'Cleanup' does not exist in the current context WebReports C:\...\WebReports\ReportsSelection.aspx.cs 109 Active"

Web app:
namespace WebReports
{
    public partial class ReportsSelection : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            String reportName = ReportName.SelectedItem.Value;
        ...
            RunSelectedReport(reportDate, reportName, entityID, await Xero_Connect(clientId, clientSecret, entityID), tenantId);
        }
        public void RunSelectedReport(DateTime reportDate, string reportName,int entityId, AccountingApi apiInstance, string xeroTenantId)
        {
            if (reportName == "Balance Sheet")
            {
                Cleanup(null, null, entityId, "[dbo].[myTable]", dataDB);
                Call_BalanceSheet(reportDate, entityId, startDate, endDate, apiInstance, xeroTenantId, accessToken, connectDB, dataDB);
            }
            
        }
        ...
    }
}

Any ideas?

Thanks.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,755
Location
Sydney, Australia
Programming Experience
10+
That's really not the way to go about it. You should create a new library project and put the common code there. Your two application projects can then both reference the library project.
 

Roust_m

Member
Joined
May 29, 2022
Messages
9
Programming Experience
Beginner
That's really not the way to go about it. You should create a new library project and put the common code there. Your two application projects can then both reference the library project.
I am beginner and the console project was created first when there were no plans for the web project. I want to make it work first by referencing the console project code. Later I may create a separate library project, but now it will take too much time separating common elements. How can I use the console project methods in web project in the current setup?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,755
Location
Sydney, Australia
Programming Experience
10+
I want to make it work first by referencing the console project code.
That would be the wrong way to go again. What you should do is extract the code into a library and make sure that the Console application works with that, then you can be sure that the web project will too. Stop trying to make a rod for your own back and do it the right way.
 

Roust_m

Member
Joined
May 29, 2022
Messages
9
Programming Experience
Beginner
That would be the wrong way to go again. What you should do is extract the code into a library and make sure that the Console application works with that, then you can be sure that the web project will too. Stop trying to make a rod for your own back and do it the right way.
This may be complicated for me and may break things. I've tried creating a class library project, but it only allows .Net 5.0. My console app is .Net Framework 4.7.2 and the web one is the same. Taking out code may be troublesome too.

Is there a way I could reference methods in web project from my console project?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,755
Location
Sydney, Australia
Programming Experience
10+
If you want to target .NET Framework then you need to create a .NET Framework project, not a .NET Core project. When you select a project template, all those that target .NET Framework specify that in the name. You can type "Framework" into the filter box to ensure you see only those.

You might want to move one class at a time to the library. The smaller each change you make, the easier it is to identify the cause of an issue if one arises.
 

Roust_m

Member
Joined
May 29, 2022
Messages
9
Programming Experience
Beginner
If you want to target .NET Framework then you need to create a .NET Framework project, not a .NET Core project. When you select a project template, all those that target .NET Framework specify that in the name. You can type "Framework" into the filter box to ensure you see only those.

You might want to move one class at a time to the library. The smaller each change you make, the easier it is to identify the cause of an issue if one arises.
Tried it, causing too many problems I don't know how to handle. I know this is not a perfect way to do it, but I just want reference the methods from console project in web project. I may later try to find out the right way, but for now I just need a quick fix.
 
Top Bottom