2013-10-22 36 views
-1

在Visual Studio 2010中視覺工作室不承認公共構造,功能

C#文件1:

namespace Level1.Level2 
    { 
     public class MyObject 
     { 

      public int _Number = 0; 
      public MyObject(int number) 
      { 
       _Number = number; 
      } 
      public System.Messaging.MessageQueue FunctionA() 
      { 
        /// 
      } 
    } 

C#文件2:

using Level1.Level2; 
namespace AnotherNS 
{ 
    public mainfunction() 
    { 
     MyObject myoj1 = new MyObject(1); 
     System.Messaging.MessageQueue SomeQueue = Level1.Level2.MyObject.FunctionA(); 
     myoj1 .FunctionA(SomeQueue); 
    } 
} 

這給了我錯誤說

Level1.Level2.MyObject不包含構造函數,需要1 arguement誤差2:Level1.Level2.MyObject犯規包含的功能誤差3的定義 :它說Level1.Level2.MyObject是 無法訪問由於其保護級別

目的是保護,但我把它改爲公衆,所以功能也是如此。 MyObject不是從任何事物繼承而來的。

任何幫助表示讚賞。非常感謝。

+2

您將至少需要改變這一行:'System.Messaging.MessageQueue SomeQueue = myObj1.FunctionA();' –

+1

確定你可以編譯第一個包含MyObject的程序集? FunctionA缺少關閉}。 – bleeeah

+0

Chris是正確的。該行意味着類/方法是靜態的。 – timmy

回答

4

參見代碼中的註釋:

namespace Level1.Level2 
{ 
    public class MyObject 
    { 

     public int _Number = 0; 
     public MyObject(int number) 
     { 
      _Number = number; 
     } 
     public System.Messaging.MessageQueue FunctionA() 
     { 
      /////// 
     //missing brace 
     } 
     } 
} 

using Level1.Level2; 
namespace AnotherNS 
{ 
    //missing class! 
    public class MyClass 
    { 
    public mainfunction() 
    { 
     MyObject myoj1 = new MyObject(1); 
     //call method from instance not as static 
     System.Messaging.MessageQueue SomeQueue = myoj1.FunctionA(); 

     //I don't even know what this is supposed to do.... 
     //myoj1 .FunctionA(SomeQueue); 
    } 
    } 
}