2015-04-04 131 views
-2

如何将一个c#文件包含到另一个c#文件中?如何将一个c#文件包含到另一个c#文件中?

我有两个c#文件就像一个是test.cs,另一个是main.cs.我想将test.cs包含到main.c中。

test.cs中文件的代码

// 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代码

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); 
    } 
} 

什么,我做错了什么?请帮帮我。

预先感谢您

+0

你应该'使用'其他类,而不是'#include'它。你为什么要包括? – 2015-04-04 07:34:24

+0

您必须在同一个项目中添加这两个文件.....并包含test.cs.这里不需要。 – 2015-04-04 07:49:13

回答

0

我们创建的与方式与其他源代码文件中声明的类对象,你已经按照:

Solution object_class = new Solution(); 

提供了解决方案类是在源代码文件中声明在同一个项目(控制台应用程序,我可以从你的帖子推断),你不必将Solution类标记为公众。否则,您应该将其标记为公共。实际上,你必须这样做才能在当前项目之外使用这个类。

你的问题,我认为是关于名称空间。你可能已经在一个文件夹中声明了这个类。无论如何,解决这个问题的方法是右键单击Solution的名称,然后单击解析引用。

1

我想你的问题是因为这两个文件不是在同一个项目中添加的。

如果您使用的是Visual Studio。

在Group类项目中添加test.cs

转到解决方案资源管理 - >添加现有项 - >浏览文件即 test.cs中 - >确定

如果您正在使用DOS模式。

确保两个文件必须位于同一个文件夹中。


而且在任何情况下。首先从主文件中删除include test.cs;。然后编译&运行

相关问题