[英]Addrange Inaccessible due to protection level?
由於保護級別的緣故,在cb.Items.AddRange(databases)的某個位置無法訪問,但是所有變量都設置為public。 它說:
C:\\ Users \\ Shulz \\ Documents \\ MySql \\ Oliver exer3 \\ showdb.cs(77,18):錯誤CS0122:由於無法訪問Sys tem.Windows.Forms.ComboBox.ObjectCollection.AddRange(System.Collections.IList)'到其保護級別C:\\ PROGRA〜2 \\ MONO-3〜1.3 \\ lib \\ mono \\ 4.5 \\ System.Windows.Forms.dll(與先前錯誤相關的sy mbol的位置)編譯失敗: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;
}
}
List<string>
不是object[]
,因此它與AddRange
不兼容,並且會出現編譯器錯誤。
在.net上,此錯誤是:
'System.Windows.Forms.ComboBox.ObjectCollection.AddRange(object[])'
的最佳重載方法匹配具有一些無效的參數參數1:無法從
'System.Collections.Generic.List<string>'
轉換為'object[]'
Mono似乎還有另一個內部重載采用IList
。 如果可以訪問此重載,那么它將與您的呼叫匹配。 因此,建議的錯誤原因有些不同。
通常,編譯器首先會注意到沒有匹配的重載。 為了幫助您,它會嘗試猜測可能的原因。 這種猜測可能會受到內部方法的影響。
我已經弄清楚了如何解決這個問題。 所以我要做的是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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.