3

How to include one c# file into another c# file?

I have two c# file like one is test.cs and another one is main.cs. I want to include test.cs into main.cs.

test.cs file code

// you can use Console.WriteLine for debugging 
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Solution
{
   public bool solution(long number1, int[,] arr1,int dim_2,int dim_3) 
    {
        //some code here
    }
}

main.cs code

using System;
include test.cs;
class Group
{
    public static void Main(String[] args)
    {
        long  number1 =  5;
        int [,] arr1 = new int[,] {{0, 0},{1, 1},{2, 2},{3, 3},{4, 4}};
        int dim_2 = 5;
        int dim_3 = 2;
        Solution object_class =  new Solution();
        bool result = object_class.solution ( number1, arr1, dim_2, dim_3 );
        Console.WriteLine("Return :");
        Console.WriteLine( result );
    }
}

what i am doing wrong here?? Please Help me.

Thank you in Advance

2
  • You should just using the other class, not #include it. Why do you want to include? Commented Apr 4, 2015 at 7:34
  • you must have to add both files in same project..... and include test.cs. is not required here. Commented Apr 4, 2015 at 7:49

4 Answers 4

2

I guess your problem is because both files are not added in the same project.

If you are using Visual Studio.

To add test.cs in the Group class project.

Go to Solution Explorer -> Add Existing item -> Browse your file i.e. test.cs -> OK

If you are using DOS mode.

Make sure that both files must be in same folder.


And in either case. first delete include test.cs; from Main file. then Compile & RUN

Sign up to request clarification or add additional context in comments.

Comments

1

I created a new .cs file, called it Class1.cs , I am working in vs code. Content of file, with one method:

namespace greeting

public class Class1
{
    public static void Hello()
    {
         Console.WriteLine("Hello there!");
               
    }
}

And simply called from file with Main method, with namespace in front:

greeting.Class1.Hello();

Comments

0

We create object of classes declared in other source code files with the way you have already followed:

Solution object_class =  new Solution();

Provided that the Solution class is declared in a source code file in the same project (console application as I can infer from your post), you don't have to mark the Solution class as a public. Otherwise, you should mark it as a public. Actually, you have to do so in case of this class is going to be used outside of your current project.

Your problem, I think is about namespaces. You might have declared this class inside a folder. Anyways, the way to solve this is to right click on the name of the Solution and then click resolve references.

Comments

0

ASP.NET C# Classes

this solution was helpfull for class files outside the App_Code you need add follow line in every page(aspx) or usercontrol(asc)

<%@ Assembly Src="~/App_Ctrl/PraxisPdfs/class_pdf.cs" %>

2 Comments

Can you clarify? This seems like an answer to a different question. Once the C# code is compiled, ASP.NET should be unaware to whether there were includes in the original C# code. Or, is your point, that it only works for code that is precompiled, but for dynamically compiled code in App_Code, you need additional steps? If so, did you mean for your Assembly directive to point to /App_Code? Some clarification would go a long way here.
My english is not so good. I wanted to show how to assign a c# class to a c# file(aspx or ascx) that is not in App_Code. The compilation of the website is static. Sorry if I didn't understand the question correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.