簡體   English   中英

不一致的可訪問性:參數類型比方法更難訪問

[英]Inconsistent Accessibility: Parameter type is less accessible than method

我正在嘗試在兩個 forms 之間傳遞一個 object(基本上是對當前登錄用戶的引用)。目前,我在登錄表單中有這些內容:

private ACTInterface oActInterface;

public void button1_Click(object sender, EventArgs e)
    {
        oActInterface = new ACTInterface(@"\\actserver\Database\Premier.pad",this.textUser.Text,this.textPass.Text);

        if (oActInterface.checkLoggedIn())
        {
            //user has authed against ACT, so we can carry on
            clients oClientForm = new clients(oActInterface);
            this.Hide();
            oClientForm.Show();
        }
        else...

在下一個表格(客戶)上,我有:

public partial class clients : Form
{
    private ACTInterface oActInt {get; set;}

    public clients(ACTInterface _oActInt)

...這導致我得到:

Error   1   Inconsistent accessibility: 
parameter type 'support.ACTInterface' is less accessible than method    
'support.clients.clients(support.ACTInterface)'  
c:\work\net\backup\support\support\clients.cs   20  16  support

我真的不明白問題出在哪里——這兩個字段都是私有的,可以通過表單中的相關公共方法訪問。 谷歌搜索並沒有真正的幫助,因為它只是指向一個元素是公共的而另一個元素是私有的,這里不是這種情況。

有人幫忙嗎?

public class clients的構造函數是public的,但它有一個ACTInterface類型的參數是private的(它嵌套在 class 中?)。 你不能那樣做。 您需要使ACTInterface至少與clients一樣可訪問。

公開 class。

class NewClass
{

}

是相同的:

internal class NewClass
{

}

所以 class 必須是公開的

如果聽起來類型ACTInterface不是public ,而是使用默認的可訪問性internal (如果它是頂級的)或private (如果它嵌套在另一種類型中)。

為類型提供public修飾符將修復它。

如果這是您的意圖,另一種方法是使類型和方法都成為internal

問題不在於字段( oActInterface ) 的可訪問性,而在於ACTInterface類型本身。

support.ACTInterface類型的可訪問性是什么。 該錯誤表明它不是公開的。

如果簽名的某些參數類型不是公共的,則不能公開公共方法簽名。 由於調用者無法構造所需的參數,因此無法從外部調用該方法。

如果您將support.ACTInterface公開,這將消除此錯誤。 如果可能的話,或者減少表單方法的可訪問性。

parameter type 'support.ACTInterface' is less accessible than method    
'support.clients.clients(support.ACTInterface)' 

錯誤說“support.ACTInterface”不太容易訪問,因為您已將接口設為私有,至少將其設為內部或公開。

問題似乎不在於變量,而在於 ACTInterface 的聲明。 ACTInterface 是否被聲明為內部?

當我收到此錯誤時,我有一個“助手”class,我沒有將其聲明為公共,這導致使用“助手”class 的 class 內部出現此問題。 將“助手” class 公開解決了此錯誤,如下所示:

公共服務類 { 公共服務類(HelperClass _helper){ } }

public class HelperClass {} // 注意解決了我的問題的公共 HelperClass。

這可能會幫助遇到這種情況的其他人。

您可以將參數(具有較少可訪問性的類)作為object ,然后通過as關鍵字將其轉換為您的 class。

在我的例子中,我在一個文件中有一個 class,我正在將那個 class 的一個實例傳遞給另一個文件中我的表單的構造函數。 問題是在沒有 public 修飾符的情況下聲明了 class: class MyClass {}

我可以通過將其更改為public class MyClass {}來解決它

如果要在新形式中使用類變量時出現此錯誤,則應將 class 定義放在

表單名.Designer.cs

而不是 Formname.cs 文件。

更新我的實體框架 model 后,我發現此錯誤感染了我的解決方案中的多個文件。 我只需右鍵單擊 my.edmx 文件和我的 TT 文件,然后單擊“運行自定義工具”,這讓我在重新啟動 Visual Studio 2012 后再次正確。

所有將ActInterface類型設為 public 的答案都是正確的。 我只是把這篇文章明確地提到為什么這是一個問題

If a parameter to your public class constructor is private or internal qualified class, it means you wont be able to create an object of that parameter class from outside of the assembly and when you cannot instantiate the parameter object, how can you call this constructor to實例化這個 class 的 object?

嘗試像這樣將您的構造函數設為私有:

private Foo newClass = new Foo();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM