2016-07-31 118 views
-1

我在同一個命名空間中有下面的代碼。from/where我可以在類中調用靜態類方法嗎?

public static class usercls 
{ 

     public static int testc() 
    { 

     int s=1; 
    } 
} 

    public class User : Page 
    { 

     private static User user;  

     int s=usercls.testc();//why not accessible here? 


    } 

我無法訪問類之外的靜態類。有人可以幫助我辨認嗎?

+0

顯示你如何試圖調用它。 – stark

+0

在用戶calss我試圖訪問它使用int s = usercls.testc(); –

+1

它應該是完全可訪問的([一旦你修復所有其他錯誤,無論如何](http://ideone.com/b0Z996))。你會得到什麼錯誤? – dasblinkenlight

回答

3

您好函數testc()不返回任何值。 它應該是這樣的

public static int testc() 
{ 
    int s=1; 
    return s; 
} 

或類似這樣的

public static int testc() 
{ 
    return 1; 
} 

你的代碼應該編譯後。

其他類無法訪問usercls的類函數,因爲編譯器沒有編譯它,因爲出現錯誤,一旦你修復了這個錯誤,它將可以從所有其他類訪問。

相關問題