简体   繁体   中英

Addrange Inaccessible due to protection level?

At somewhere cb.Items.AddRange(databases) there is inaccessibility due to protection level but all variables were set to public. Here it says:

C:\\Users\\Shulz\\Documents\\MySql\\Oliver exer3\\showdb.cs(77,18): error CS0122: `Sys tem.Windows.Forms.ComboBox.ObjectCollection.AddRange(System.Collections.IList)' is inaccessible due to its protection level C:\\PROGRA~2\\MONO-3~1.3\\lib\\mono\\4.5\\System.Windows.Forms.dll (Location of the sy mbol related to previous error) Compilation failed: 1 error(s), 0 warnings

    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;
    }
}

A List<string> is not an object[] , so it's not compatible with AddRange and you get a compiler error.

On .net this error is:

The best overloaded method match for 'System.Windows.Forms.ComboBox.ObjectCollection.AddRange(object[])' has some invalid arguments

Argument 1: cannot convert from 'System.Collections.Generic.List<string>' to 'object[]'

Mono seems to have another internal overload taking an IList . If this overload were accessible, it'd match your call. So the suggested cause for the error is a bit different.

In general the compiler first notices that there is no matching overload. To help you, it tries to guess at the likely cause. This guess can be affected by the presence of internal methods.

I've figured out how to solve this problem. So what I did is db.ToArray() then assinged my databases which is object. It was just simple as that.

    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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM