【问题标题】:Addrange Inaccessible due to protection level?Addrange 由于保护级别而无法访问?
【发布时间】:2015-01-18 15:32:23
【问题描述】:

在某处 cb.Items.AddRange(databases) 由于保护级别而无法访问,但所有变量都设置为公共。 这里说:

C:\Users\Shulz\Documents\MySql\Oliver exer3\showdb.cs(77,18):错误 CS0122:`系统 tem.Windows.Forms.ComboBox.ObjectCollection.AddRange(System.Collections.IList)' 由于其保护级别而无法访问 C:\PROGRA~2\MONO-3~1.3\lib\mono\4.5\System.Windows.Forms.dll(位置 与先前错误相关的符号)编译失败:1 错误,0 个警告

    public ComboBox cb;
    public Label label;
    public object[] databases;

    public MForm() {

       string connectionString =
          "Server=localhost;" +
          "Database=information_schema;" +
          "User ID=root;" +
          "Password=root;" +
          "Pooling=false";
       IDbConnection dbcon;
       dbcon = new MySqlConnection(connectionString);
       dbcon.Open();
       IDbCommand dbcmd = dbcon.CreateCommand();

        string sql = "SELECT COUNT(*) as count FROM information_schema.SCHEMATA"; //count the databases(string) and names it count
        dbcmd.CommandText = sql;                        //sends the string to sql
        IDataReader reader = dbcmd.ExecuteReader();     //assign the function to reader
        reader.Read();                                  //uses its getter(Read)
        int count = Convert.ToInt32(reader["count"]);   //converts the "count"(column) to integer

        reader.Close();
        reader = null;
        dbcmd.Dispose();
        dbcmd = null;



        dbcmd = dbcon.CreateCommand();
        sql = "show databases";
        dbcmd.CommandText = sql;
        reader = dbcmd.ExecuteReader();

        var databases = new List<string>();

        var excludeDatabases = new List<string> { "information_schema","sakila","enrollmentsystem","mysql","world","performance_schema" };

        while(reader.Read())
        {
            var data = reader["Database"].ToString();

            if(!excludeDatabases.Contains(data)){
            databases.Add(data);
            }
        }

       reader.Close();
       reader = null;
       dbcmd.Dispose();
       dbcmd = null;
       dbcon.Close();
       dbcon = null;




        Text = "School Year";
        Size = new Size(340, 240);

        cb = new ComboBox();
        cb.Parent = this;
        cb.Location = new Point(50, 30);

        cb.Items.AddRange(databases);

        cb.SelectionChangeCommitted += new EventHandler(OnChanged);

        label = new Label();
        label.Location = new Point(80, 170);
        label.Parent = this;
        label.Text = "...";

        CenterToScreen();




    }

    void OnChanged(object sender, EventArgs e) {
         ComboBox combo = (ComboBox) sender;
         label.Text = combo.Text;
    }
}

【问题讨论】:

  • 看起来您正在使用 Mono。查看您的错误消息,我可以说您的项目可能是组合框的只读或私有属性。可能还有其他方法可以为组合框设置数据源,如下所示:combobox.DataSource = YourData;
  • 您传入的是List&lt;T&gt;,但该方法需要object[]
  • 是的。我正在使用单声道编译器。那么如何将其转换为 object[] ?

标签: c# protection


【解决方案1】:

List&lt;string&gt; 不是 object[],因此它与 AddRange 不兼容,并且会出现编译器错误。

在 .net 上,此错误是:

'System.Windows.Forms.ComboBox.ObjectCollection.AddRange(object[])' 的最佳重载方法匹配有一些无效参数

参数 1:无法从 'System.Collections.Generic.List&lt;string&gt;' 转换为 'object[]'

Mono 似乎有另一个内部重载采用IList。如果可以访问此重载,它将与您的调用相匹配。所以建议的错误原因有点不同。

通常编译器首先注意到没有匹配的重载。为了帮助您,它会尝试猜测可能的原因。这种猜测可能会受到内部方法的影响。

【讨论】:

  • 内部过载是什么意思?
【解决方案2】:

我已经想出了如何解决这个问题。所以我所做的是 db.ToArray() 然后分配我的数据库是对象。就是这么简单。

    var db = new List<string>();

    var excludeDatabases = new List<string> { "information_schema","sakila","enrollmentsystem","mysql","world","performance_schema" };

    while(reader.Read())
    {
        var data = reader["Database"].ToString();

        if(!excludeDatabases.Contains(data)){
        db.Add(data);

        }
    }

   reader.Close();
   reader = null;
   dbcmd.Dispose();
   dbcmd = null;
   dbcon.Close();
   dbcon = null;




    Text = "School Year";
    Size = new Size(340, 240);

    cb = new ComboBox();
    cb.Parent = this;
    cb.Location = new Point(50, 30);


    databases = db.ToArray();
    cb.Items.AddRange(databases);

【讨论】:

    猜你喜欢
    • 2015-09-26
    • 2011-09-01
    • 1970-01-01
    • 2011-04-02
    • 2011-02-07
    • 2012-01-25
    • 2022-01-18
    相关资源
    最近更新 更多