2015-04-04 47 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;。然後編譯&運行

相關問題